I made a program about caesar cipher but it gives me this error when I am trying to change shift value:
shifted_value = shifted_value + user_input4
^^^^^^^^^^^^^
NameError: name 'shifted_value' is not defined
Code
custom_alphabet = "NİDALQWERTYUIOPĞÜSFGHJKŞZXCVBMÖÇnidalüğpoıuytrewqşkjhgfsçömbvcxz37890712456+=-/%()*$&"
def three_shift_encrypt(text, alphabet):
for character in text: #characters contain all of the chars in text
global shifted_value
shifted_value = alphabet.find(character) + 3
print(custom_alphabet[shifted_value], end='')
def three_shift_decrypt(text2, alphabet2):
for character2 in text2:
global shifted_value2
shifted_value2 = alphabet2.find(character2) - 3
print(custom_alphabet[shifted_value2], end='')
print('To exit the program press Ctrl + C')
while True:
user_input1 = input('encrypt or decrypt or change shitf value?(e/d/change) -->')
if user_input1 == 'e':
user_input2 = input('enter plaintext -->')
three_shift_encrypt(user_input2, custom_alphabet)
print('n')
elif user_input1 == 'd':
user_input3 = input('enter plaintext -->')
three_shift_decrypt(user_input3, custom_alphabet)
print('n')
elif user_input1 == 'change':
user_input4 = input('enter the new shift value (int) -->')
shifted_value = shifted_value + user_input4
shifted_value2 = shifted_value2 + user_input4
else:
print('please enter e or d')
break
Actually the shifted values were not global. I tried to make it global but it didn’t work.
Why am I getting this error?
New contributor
stfwq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5