from tkinter import *
master = Tk()
master.title("Caeser Cipher Program")
master.geometry("300x200")
frame1 = Frame(master)
frame2 = Frame(master)
frame3 = Frame(master)
frame4 = Frame(master)
frame5 = Frame(master)
frame6 = Frame(master)
global encryptedText
global file
global shiftKey
file = ""
encryptedText = ""
removeSpaces = ""
def Start():
frame1.grid()
Question = Label(frame1, text = "Would you like to encrypt or decrypt a statement?\n\n\n")
Question.grid(row = 0, column = 1)
ButtonPlace1 = Button(frame1, text = "Encrypt", command = EncryptChosen)
ButtonPlace2 = Button(frame1, text = "Decrypt", command = decrypt_command)
ButtonPlace1.place(x = 75, y = 50, anchor = "c")
ButtonPlace2.place(x = 175, y = 50, anchor = "c")
def EncryptChosen():
frame1.destroy()
frame2.grid()
Question = Label(frame2, text = "What would you like to shift it by?\t\t\t\n\n\n\n\n ")
ButtonPlace3 = Entry(frame2)
def SubmitEncryptionKey():
shiftKey = ButtonPlace3.get()
frame2.destroy()
frame3.grid()
Question = Label(frame3, text = "What would you like it to say?\t\t\t\n\n\n\n\n" )
ButtonPlace5 = Entry(frame3)
def SubmitText():
file = ButtonPlace5.get()
frame3.destroy()
frame4.grid()
Question = Label(frame4, text = "Would you like to remove spaces?\t\t\t\n\n\n\n\n")
Question.grid(row = 0, column = 1)
def doRemoveSpaces():
global spacesStatement
global removeSpaces
spacesStatement = "spaces will be removed"
removeSpaces = "True"
ReadyToEncrypt()
ButtonPlace7 = Button(frame4, text = "Yes", command = doRemoveSpaces)
def doNotRemoveSpaces():
global spacesStatement
global removeSpaces
spacesStatement = "spaces will not be removed"
removeSpaces = "False"
ReadyToEncrypt()
ButtonPlace8 = Button(frame4, text = "No", command = doNotRemoveSpaces)
def ReadyToEncrypt():
frame4.destroy()
frame5.grid()
Question = Label(frame5, text = file + "\n will be encrypted by " + shiftKey + " and \n" + spacesStatement + ".\t\t\t\n\n\n\n")
Question.grid(row = 0, column = 1)
def encryptSetup():
def encrypt(character):
alphabet = 'abcdefghijklmnopqrstuvwxyz '
character = character.lower()
if character.isalpha():
position = str(alphabet).find(character) + shiftKey
if position > 25:
position -= 26
return alphabet[position]
else:
return character
for line in file:
for c in line:
global encryptedText
encryptedText = encryptedText + encrypt(c)
if removeSpaces == "True":
encryptedText = encryptedText.replace(" ","")
frame5.destroy()
frame6.grid()
Question = Label(frame6, text = "Here is your encrypted message: \n" + encryptedText)
Question.grid(row = 0, column = 1)
ButtonPlace9 = Button(frame5, text = "Encrypt!", command = encryptSetup)
ButtonPlace9.place(x=175,y=50)
ButtonPlace7.place(x = 75, y = 50, anchor = "c")
ButtonPlace8.place(x = 175, y = 50, anchor = "c")
ButtonPlace6 = Button(frame3, text = "Submit", command = SubmitText)
Question.grid(row = 0, column = 1)
ButtonPlace5.place(x=50,y=50)
ButtonPlace6.place(x=175,y=45)
ButtonPlace4 = Button(frame2, text = "Submit", command = SubmitEncryptionKey)
Question.grid(row = 0, column = 1)
ButtonPlace3.place(x=50,y=50)
ButtonPlace4.place(x=175,y=45)
frame1.grid()
Question = Label(frame1, text = "Would you like to encrypt or decrypt a statement?\n\n\n")
Question.grid(row = 0, column = 1)
ButtonPlace1 = Button(frame1, text = "Encrypt", command = EncryptChosen)
ButtonPlace2 = Button(frame1, text = "Decrypt", command = "")
ButtonPlace1.place(x = 75, y = 50, anchor = "c")
ButtonPlace2.place(x = 175, y = 50, anchor = "c")
master.loop()
这是我的程序代码,允许您以caeser cypher的形式加密或解密语句。到目前为止,我已对其进行编码,以便您通过它,选择您希望它移动的内容,您想要加密的内容,以及是否要删除空格。我还没有开始解密。但是,在代码中,在收集了所有必要的变量之后,实际上加密了我遇到问题的语句。我创建了一个名为' alphabet'存储整个字母表。当函数运行时,我需要找到字符在字母表中的位置,以便我可以移动它。但是,当我这样做并运行它时,我得到一个错误:
position = alphabet.find(character) + shiftKey
TypeError: unsupported operand type(s) for +: 'int' and 'str'
另外,当我使用for循环运行整个语句并加密它时,我遇到了这个错误:
UnboundLocalError: local variable 'encryptedText' referenced before assignment
这是一个问题,因为我需要做的就是将每个加密字符添加到加密文本字符串中,但这阻止了我这样做。请允许有人以任何方式提供帮助,因为我需要在2017年3月9日星期一完成这个项目。
答案 0 :(得分:1)
alphabet.find(chr)
返回一个整数,shiftkey
是一个字符串 - 更改一个或另一个以匹配。
encrypted_text
是其函数中的局部变量 - 您在全局范围顶部的global encrypted_text
声明是无用的。 global
意味着在函数内部使用,告诉Python列出的变量名不是函数的本地名称,而是在全局范围内找到它。