我正在尝试制作一个加密python程序,但我无法让它工作。我已经将输入和输出函数添加到最后但是我收到了一条错误消息,我在下面发布了该消息。
这是我试过的
#Caesar Cipher
MAX_KEY_SIZE = 26
def getMode():
while True:
mode = raw_input('Do you wish to encrypt or decrypt a message? ')
mode = mode.lower()
if mode in "encrypt e decrypt d":
return mode
else:
print 'Enter either \"encrypt\" or \"e\" or \"decrypt\" or \"d\ ".'
def getMessage():
def getMessage():
return raw_input('Enter you message ')
def getKey():
key = 0
while True:
key = int(raw_input('Enter a key number (1-26) '))
if key >=1 and key <= MAX_KEY_SIZE:
return key
def getTranslatatedMessage(mode, message, key):
if mode[0] == "d":
key = -key
translated = ""
for symbol in message:
if symbol.isalpha():
num = ord(symbol)
num += key
if symbol.isupper():
if num > ord("Z"):
num -= 26
elif num < ord("A"):
num += 26
elif symbol.islower():
if num > ord("z"):
num -= 26
elif num < ord("a"):
num += 26
translated += chr(num)
else:
translated += symbol
return translated
mode = getMode()
message = getMessage()
key = getKey()
print 'Translated message: ', getTranslatatedMessage(mode, message, key)
f = open("temp.txt", "w")
f.write("Hello!\n")
f.close
#open file to read this time
f = open("temp.txt", "r")
line = f.readline()
print(line)
f.close
#try binary read...
f = open("temp.txt", "rb")
line = f.readlines()
print(line)
f.close
if "d" in mode:
encrypted = EncyptMessage(message,key) #you will need to make this
f.open("temp.txt","w")
f.write(encrypted)
else:
unencrypted = getTranslatatedMessage(mode, message, key)
f.open("temp.txt","w")
f.close
我得到一个错误说:
Do you wish to encrypt or decrypt a message? encrypt
Enter a key number (1-26) 1
Translated message:
Traceback (most recent call last):
File "/Users/Ally/Documents/Python Programs/EncryptDecrypt Program-Enhanced", line 50, in <module>
print 'Translated message: ', getTranslatatedMessage(mode, message, key)
File "/Users/Ally/Documents/Python Programs/EncryptDecrypt Program-Enhanced", line 27, in getTranslatatedMessage
for symbol in message:
TypeError: 'NoneType' object is not iterable
>>>
答案 0 :(得分:1)
看起来您最终会想要它,因为您需要定义所有函数并调用所有函数。我不完全确定这是你要问的,但这会奏效。我也进入了我认为应该为翻译和解密做的事情。
MAX_KEY_SIZE = 26
def getMode():
while True:
mode = raw_input("Do you wish to encrypt or decrypt a message?").lower() #note I am using `raw_input`
if mode in ["encrypt", "e", "decrypt", "d"]:
return mode
else:
print('"Enter either "encrypt" or "e" or "decrypt" or "d".')
getMode() # Note this change
def getMessage():
return input("Enter you message")
def getKey():
key = 0
while True:
key = int(input("Enter a key number (1-26)"))
if (key >=1 and key <= MAX_KEY_SIZE):
return key
def getTranslatatedMessage(mode, message, key):
if mode[0] == "d":
key = -key
translated = ""
for symbol in message:
if symbol.isalpha():
num = ord(symbol)
num += key
if symbol.isupport():
if num > ord("Z"):
num -= 26
elif num < ord("A"):
num += 26
elif symbol.islower():
if num < ord("z"):
num += 26
elif num < ord("a"):
num += 26
translated += chr(num)
else:
translated += symbol
return translated
mode = getMode()
message = getMessage()
key = getKey()
f = open("temp.txt", "w")
f.write("Hello!\n")
f.close
#open file to read this time
f = open("temp.txt", "r")
line = f.readline()
print(line)
f.close
#try binary read...
f = open("temp.txt", "rb")
line = f.readlines()
print(line)
f.close
if "d" in mode:
encrypted = "Later change encrypted to equal the function->" #EncryptMessage(message,key) #you will need to make this
f.open("temp.txt","w")
f.write(encrypted)
else:
unencrypted = getTranslatatedMessage(mode, message, key)
f.open("temp.txt","w")
f.close
答案 1 :(得分:0)
getTranslatatedMessage(mode, messafe, key):
应该是
getTranslatatedMessage(mode, message, key):