Python 为什么encrypt()不返回任何值?

Python 为什么encrypt()不返回任何值?,python,Python,我知道让一帮人看几段代码有点大胆,但我还是会试试。我试图用python和tk创建enigma(二战中使用的德国加密机)的克隆。从逻辑上看我的代码,方法encrypt()要么返回字符串“Alert”(可能性很小),要么返回None。有没有人能给它一个快速但专注的一瞥,这样我就可以解决这个问题?多谢各位 from Tkinter import * from string import letters import tkMessageBox root = Tk() root.title("Enigm

我知道让一帮人看几段代码有点大胆,但我还是会试试。我试图用python和tk创建enigma(二战中使用的德国加密机)的克隆。从逻辑上看我的代码,方法
encrypt()
要么返回字符串“Alert”(可能性很小),要么返回
None
。有没有人能给它一个快速但专注的一瞥,这样我就可以解决这个问题?多谢各位

from Tkinter import *
from string import letters
import tkMessageBox

root = Tk()
root.title("EnigmaTK")

def rank(x, d = dict((letr,n%26+1) for n,letr in enumerate(letters[0:52]))):
    return d[x]


def shift(key, array):
    counter = range(len(array))
    new = counter
    for i in counter:
        new[i] = array[i-key]
    return new

alph = ["a", "b", "c", "d", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
rotI = ["a", "b", "c", "d", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
rotII = ["a", "b", "c", "d", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
rotIII = ["a", "b", "c", "d", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
ref = ["a", "b", "c", "d", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "e", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]

label = Label(root, text="Input:")
label.pack(side = LEFT)
entry = Entry(root)
entry.pack(side = RIGHT)
input = entry.get()
rotor_set = map(rank, input[:3])
message = input[3:]


def encrypt():
    new_message = message
    for a in xrange(len(message)):
        for e in range(rotor_set[2]):
            new_message[a] = alph[rotI.index(rotII[rotIII.index(ref[rotIII.index(rotII[rotI.index(alph[a])])])])]
            a = a + 1
            rotIII = shift(1, rotIII)
        for i in range(rotor_set[1]):
            new_message[a] = alph[rotI.index(rotII[rotIII.index(ref[rotIII.index(rotII[rotI.index(alph[a])])])])]
            a = a + 1
            rotII = shift(1, rotII)
        for o in range(rotor_set[0]):
            new_message[a] = alph[rotI.index(rotII[rotIII.index(ref[rotIII.index(rotII[rotI.index(alph[a])])])])]
            a = a + 1
            rotI = shift(1, rotI)
    return new_message


def show():
    tkMessageBox.showinfo( "English to Enigma", encrypt())


e = Button(root, text = "encrypt", command = show)
e.pack()
root.mainloop()

靠近顶部的字母表都是一样的。如果问题解决了,我会改变这个。谢谢。

我猜
加密
正在抛出一个异常,该异常被Tk转换为警报。如果从命令行启动程序,您可能会看到异常

from Tkinter import *
from string import letters
import tkMessageBox

root = Tk()
root.title("EnigmaTK")

def rank(x, d = dict((letr,n%26+1) for n,letr in enumerate(letters[0:52]))):
    return d[x]


def shift(key, array):
    counter = range(len(array))
    new = counter
    for i in counter:
        new[i] = array[i-key]
    return new

alph = ["a", "b", "c", "d", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
rotI = ["a", "b", "c", "d", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
rotII = ["a", "b", "c", "d", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
rotIII = ["a", "b", "c", "d", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
ref = ["a", "b", "c", "d", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "e", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]

label = Label(root, text="Input:")
label.pack(side = LEFT)
entry = Entry(root)
entry.pack(side = RIGHT)
input = entry.get()
rotor_set = map(rank, input[:3])
message = input[3:]


def encrypt():
    new_message = message
    for a in xrange(len(message)):
        for e in range(rotor_set[2]):
            new_message[a] = alph[rotI.index(rotII[rotIII.index(ref[rotIII.index(rotII[rotI.index(alph[a])])])])]
            a = a + 1
            rotIII = shift(1, rotIII)
        for i in range(rotor_set[1]):
            new_message[a] = alph[rotI.index(rotII[rotIII.index(ref[rotIII.index(rotII[rotI.index(alph[a])])])])]
            a = a + 1
            rotII = shift(1, rotII)
        for o in range(rotor_set[0]):
            new_message[a] = alph[rotI.index(rotII[rotIII.index(ref[rotIII.index(rotII[rotI.index(alph[a])])])])]
            a = a + 1
            rotI = shift(1, rotI)
    return new_message


def show():
    tkMessageBox.showinfo( "English to Enigma", encrypt())


e = Button(root, text = "encrypt", command = show)
e.pack()
root.mainloop()
如果这是一个例外,我猜您正试图通过执行
new\u message[a]=…
来修改
new\u message
。如果
new\u message
是字符串(或元组),则在Python中不允许这样做,因为字符串是不可变的。相反,请使用以下列表:

new_message = list(message)
并在返回时加入邮件:

return ''.join(new_message)

这一行似乎在错误的时间执行:

input = entry.get()
我假定您希望在用户单击按钮时获取数据,而不是在程序运行开始时。由于用户单击按钮时不提取数据,
message
仍为空字符串,
new\u message
变为空字符串,
encrypt()
返回空字符串

一种可能的解决方案是修改
encrypt()