2015-05-29 70 views
2

由於某些原因,當我在末尾運行代碼時它只是顯示消息而沒有加密或解密它我真的很困惑如果這真的很明顯,請不要討厭我很新的蟒蛇,幾乎掌握了基礎知識爲什麼我的python代碼不能加密或解密我的消息

#declare variables 
NewWord ="" 
NewLetter = "" 
SecretMessage = 0 

mode = input("Please enter a mode: ").lower() #makes it lowercase 
message = input("Please enter a message: ").lower() # lowercase to tackle capitals 
while True: 
    try: 
     offset = int(input("Please enter a number: ")) #If no exception occurs, the except clause is skipped and execution of the try statement is finished. 
     break 
    except ValueError: #If exception occurs, the except clause continues printing not a valid number and letting you re-enter the offset and not just throwing up an error. 
     print ("Not a valid number") 
#print(mode, message, offset) #Test to check user Input 

for letter in message : 
    SecretMessage = ord(letter) 
    if mode == "Encrypt" : 
     SecretMessage += offset # add the offset to the letter 
    if mode == "Decrypt" : 
     SecretMessage -= offset # subtract the offset to the letter 
    if SecretMessage < 97: 
     SecretMessage += 26 
    if SecretMessage > 122: 
     SecretMessage -= 26 
    NewLetter = SecretMessage 
    NewLetter = chr(NewLetter) 

    # print(newLetter)# check conversion 
    NewWord += NewLetter 

print(NewWord) 

回答

0
mode = input("Please enter a mode: ").lower() 

使得模式全部小寫防止它等於無論是「加密」或「解密」。所以,您的if子句不會被執行。

0

您正在降低mode變量,因此無法等於EncryptDecrypt。您必須測試這些字的小寫版本:

if mode == "encrypt" : 
    SecretMessage += offset # add the offset to the letter 
if mode == "decrypt" : 
    SecretMessage -= offset # subtract the offset to the letter 
相關問題