input - Caesar Cipher in python -
the error getting is
traceback (most recent call last): file "imp.py", line 52, in <module> mode = getmode() file "imp.py", line 8, in getmode mode = input().lower() file "<string>", line 1, in <module> nameerror: name 'encrypt' not defined
below code.
# caesar cipher max_key_size = 26 def getmode(): while true: print('do wish encrypt or decrypt message?') mode = input().lower() if mode in 'encrypt e decrypt d'.split(): return mode else: print('enter either "encrypt" or "e" or "decrypt" or "d".') def getmessage(): print('enter message:') return input() def getkey(): key = 0 while true: print('enter key number (1-%s)' % (max_key_size)) key = int(input()) if (key >= 1 , key <= max_key_size): return key def gettranslatedmessage(mode, message, key): if mode[0] == 'd': key = -key translated = '' 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('your translated text is:') print(gettranslatedmessage(mode, message, key))
the problem here:
print('do wish encrypt or decrypt message?') mode = input().lower()
in python 2.x input use raw_input()
instead of input()
.
python 2.x:
- read string standard input:
raw_input()
- read string standard input , evaluate it:
input()
.
python 3.x:
- read string standard input:
input()
- read string standard input , evaluate it:
eval(input())
.
Comments
Post a Comment