我曾嘗試爲我在學校的首個Python項目製作凱撒密碼。我有點從主密碼段的YouTube視頻中複製出代碼,但是當我加密用戶鍵入的消息時,它會執行隨機密碼,而不是輸入到shell中的密鑰。這是代碼:凱撒密碼不正確旋轉字母? (Python)
abc = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
def main():
message = input("What's the message to encrypt/decrypt? ")
key = int(input("What number would you like for your key value? "))
choice = input("Choose: encrypt or decrypt. ")
if choice == "encrypt":
encrypt(message, key)
elif choice == "decrypt":
encrypt(message, key * (-1))
else:
print("Bad answer, try again.")
def encrypt(message, key):
cipherText = ""
for letter in message:
if letter in abc:
newPosition = (abc.find(letter) + key) % 26
cipherText += abc[newPosition]
else:
cipherText += letter
print(cipherText)
return cipherText
main()
有人可以幫我解決這個問題,請。另外請不要讓它變得非常複雜,因爲我是Python的初學者,而且我一點都不瞭解。
謝謝!
看看[ask] – pvg
你沒有將'key'或'message'傳遞給'encrypt'方法。 – KDecker
'encrypt'方法有兩個參數。 – GAVD