Restarting code in Python 3.1
我在这里看了几个答案,但没有一个对我有用,我知道这个代码可能有很多问题,但我想知道的是如果在END处输入的话只需重新启动代码 是是的'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | Code = input("Do you want to encrypt or decrypt?") Code = Code.upper() Answer = ["ENCRYPT","DECRYPT"] if Code in Answer: Plaintext = input("What's your message?") Plaintext = Plaintext.upper() Shift = int(input("What's the shift number?")) LengthPT = len(Plaintext) CodeLetter ="" if Code == ("ENCRYPT"): for i in range (0,LengthPT): Pletter = ord(Plaintext[i]) -64 Codeletter = Pletter + Shift if Codeletter > 26: Codeletter = Codeletter - 26 Codeascii = chr(Codeletter + 64) CodeLetter = CodeLetter + Codeascii elif Code == ("DECRYPT"): for i in range (0,LengthPT): Pletter = ord(Plaintext[i]) -64 Codeletter = Pletter - Shift if Codeletter < 0: Codeletter = Codeletter + 26 Codeascii = chr(Codeletter + 64) CodeLetter = CodeLetter + Codeascii else: print("Wrong answer.") if Code == ("ENCRYPT"): print("Encoded Message =", CodeLetter) elif Code == ("DECRYPT"): print("Decoded Message =", CodeLetter) Answer2 = input("Do you want to restart? (Yes/No):") |
有很多方法,但这似乎是最短路径:
1 2 3 4 | Answer2 ="Yes" while Answer2 =="Yes": ... Answer2 = input("Do you want to restart? (Yes/No):") |
通常,您可能也希望
让它成为一个功能。 检查答案是否为"是",然后再次调用该功能。 递归在Python中很好,所以这将起作用。