2

大家好,我有一个问题。为什么以下代码不产生任何输出或错误?

这是对用户代码进行加密和解密的代码

alpha = ['a','b','c','d','e','f',
     'g','h','i','j','k','l',
     'm','n','o','p','q','r',
     's','t','u','v','w','x',
     'y','z']

shift = range(26)

def user_info():
    info = input("\nPress 'e' to encrypt or 'd' to decrypt: ").lower()
    if info == 'e' or 'd':
       return info

def user_message():
    code = input("What is your message?: ")
    return code

def user_shift():
     shift = int(input("What is your shift number?: "))
     while True:
         if shift == int(shift):
             return shift

def True_Message(info, code, shift):

    if info[0] == 'd':     #This encrypts the code 
         shift = -shift    

    for letter in code:
        if letter in alpha:
        alpha_2 = ord(letter) + shift
        secret_message = ""
    if alpha_2 in range (0, len(alpha)):
        final_mix = chr(alpha)
        secret_message += final_mix

return secret_message

info = user_info()
code = user_message()
shift = user_shift()
print(True_Message(info, code, shift))

没有任何错误,所以我不知道出了什么问题。我是这个加密的新手,我不确定这是否在正确的轨道上,有什么想法吗?谢谢你。

更新!!!!!

抱歉,我希望代码能够根据用户的选择加密和解密代码

4

4 回答 4

3

您需要检查您的缩进和or运算符的使用情况:

alpha = ['a','b','c','d','e','f',
     'g','h','i','j','k','l',
     'm','n','o','p','q','r',
     's','t','u','v','w','x',
     'y','z']

shift = range(26)

def user_info():
    info = input("\nPress 'e' to encrypt or 'd' to decrypt: ").lower()
    if info in ('e', 'd'): # 'or' does not work how you think it does
        return info

def user_message():
    code = input("What is your message?: ")
    return code

def user_shift():
     while True:
         shift = int(input("What is your shift number?: "))
         if shift == int(shift):
             return shift

def True_Message(info, code, shift):

    if info[0] == 'd':     #This encrypts the code 
         shift = -shift    

    for letter in code:
        if letter in alpha:
            alpha_2 = ord(letter) + shift
            secret_message = ""
    if alpha_2 in range (0, len(alpha)):
        final_mix = chr(alpha)
        secret_message += final_mix

    return secret_message
于 2013-03-24T00:37:28.173 回答
1

Python 是一种对缩进敏感的语言。你到处都有缩进错误。

你怎么能说“没有任何错误” - 你在运行脚本吗?python 解释器会告诉你它们的确切位置。当我运行你的脚本时:

C:\Users\Jonathon\temp>python caesar.py
  File "caesar.py", line 22
    return shift
         ^
IndentationError: expected an indented block

所以你的第一个错误在这里:

def user_shift():
     shift = int(input("What is your shift number?: "))
     while True:
         if shift == int(shift):
         return shift              # ---> Should be indented one more level
于 2013-03-24T00:31:28.363 回答
1

正如其他人所说,你有一些缩进问题。

对于逻辑级别,您应该明确检查是否secret_message = ""是您想要的。其他要点:

  • 如果letterZ并且你添加了一个班次怎么办?(检查 ASCII 表
  • 您确定要让班次range(26)排在首位吗?尝试print(list(range(26)))查看范围的情况。

不要气馁,玩 ASCII 起初可能会令人沮丧,但一旦熟悉它会非常有趣!

于 2013-03-24T00:53:04.083 回答
-3

不,看看这个。

from tkinter import *

                        #Functions. The first three do the job. The rest are about GUI
def wordlist(a):
    word_list=(list(a))
    return (word_list)

def length(a):
    return(len(wordlist(a))-1)

def encrypt(a,b):
    thelist=wordlist(a)
    i=0
    while i<=(length(a)):
        thelist[i]=(chr(ord(wordlist(a)[i])+(b)))
        i=i+1
    return (''.join(thelist))

def callback():    
    e.focus_set()
    a=e.get()

    e2.focus_set()
    b=int(e2.get())

    e3.delete(0, (length(a)+1))
    e3.insert(0,(encrypt(a,b)))

                        #User Interface

root = Tk(className="_Caesar Cipher Encryptor")


frame1 = Frame(root)
frame1.pack()

frame2= Frame(root)
frame2.pack()

frame3=Frame(root)
frame3.pack()


enter=Label(frame1, text="Enter Text:", bd=3, font="Ariel")
enter.pack(side=LEFT,fill=X)

shift=Label(frame2, text="Shift:", font="Ariel")
shift.pack(side=LEFT,fill=X)

result=Label(frame3,text="Encrypted Code:", font="Ariel")
result.pack(side=LEFT)


e= Entry(frame1,width=50, font="Ariel")
e.pack(side=RIGHT)

e2= Entry(frame2,width=10, font="Ariel")
e2.pack(side=LEFT)

e3=Entry(frame3, font="Ariel")
e3.pack(side=RIGHT)


b = Button(frame2, text="Encrypt", width=10, command=callback, font="Ariel")
b.pack(side=RIGHT)


mainloop()
于 2015-02-01T10:20:04.970 回答