代码是这样的:
'''
Caesar Cipher
'''
while True:
choice = str(input('You want to encrypt or decrypt?\na ). encrypt\nb ). decrypt\n')).lower()
if choice == 'a':
mode = 'encrypt'
message = str(input('Input the message you want to encrypt:'))
if message == 'quit':
print('\n')
break
else:
mode = 'decrypt'
message = str(input('Input the message you want to decrypt:'))
if message == 'quit':
print('\n')
break
try:
key = int(input('Input the encrypt key:'))
except ValueError:
print('Input wrong message or key!')
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopgrstuvwxyz1234567890 !?.:'
translated = ''
for symbol in message:
if symbol in SYMBOLS:
SymbolIndex = SYMBOLS.find(symbol)
if mode == 'encrypt':
TranslatedIndex = SymbolIndex + key
else:
TranslatedIndex = SymbolIndex - key
if TranslatedIndex > len(SYMBOLS):
TranslatedIndex = TranslatedIndex - len(SYMBOLS)
elif TranslatedIndex < 0:
TranslatedIndex = TranslatedIndex + len(SYMBOLS)
translated += SYMBOLS[TranslatedIndex]
else:
translated += symbol
print(translated)
当message = 'message',key = 12时,加密结果为yg55msg,解密后竟然变成了mUssagU。
求大佬解答,悬赏5鱼币。
低级错误:'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopgrstuvwxyz1234567890 !?.:
|