我知道让一帮人看几段代码有点大胆,但我还是会试试的。我正在尝试用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()
靠近顶部的字母表都是一样的。如果问题解决了,我会改变这个。非常感谢。你知道吗
我的猜测是
encrypt
正在抛出一个异常,该异常被Tk转换为警报。如果从命令行启动程序,可能会看到异常。你知道吗如果这是一个例外,我的猜测是您正试图通过执行
new_message[a] = ...
来修改new_message
。如果new_message
是一个字符串(或元组),这在Python中是不允许的,因为字符串是不可变的。相反,使用列表:返回时加入邮件:
这一行似乎在错误的时间执行:
我假设您希望在用户单击按钮时获取数据,而不是在程序运行开始时。由于用户单击按钮时不提取数据,
message
仍然是空字符串,new_message
成为空字符串,encrypt()
返回空字符串。你知道吗一个可能的解决方案是修改
encrypt()
:相关问题 更多 >
编程相关推荐