I'm fairly new in programming with python and I've been trying to make the caeser cipher encryption method, but when I debug and run through the program, I notice that some if statements are not being ran through even though all the conditions are met for some letters. I try to make sure that it doesn't give me a random symbol, and stays in range A-Z and a-z.
Here's my code:
# Caeser Encryption Method
def index_higher_90(shift, original_letter):
shift = shift % 26
new_character_revised = (ord(original_letter) - 65 + shift) % 26 + 65
return chr(new_character_revised)
# shift = shift % 26
# print("shift is ", shift)
# new_shift = shift
# new_character_revised = (ord(original_letter) + new_shift)
# new_character_revised = chr(new_character_revised)
# return new_character_revised
def index_higher_122(shift, original_letter):
shift = shift % 26
new_character_revised = (ord(original_letter) - 97 + shift) % 26 + 97
return chr(new_character_revised)
# shift = shift % 26
# new_shift = shift
# new_character_revised = (ord(original_letter) + new_shift)
# new_character_revised = chr(new_character_revised)
# return new_character_revised
def encrypt():
ciphertext = ""
plaintext = input("Enter plaintext and I'll cipher it: ")
shift = int(input("Set the shift index: "))
for i in plaintext:
if i.isalpha():
char_code = ord(i)
new_index_number = char_code + shift
new_character = chr(new_index_number)
if not new_character.isalpha() and i.isalpha() and i.isupper():
if new_index_number > 90:
new_character_revised = index_higher_90(shift, i)
ciphertext += new_character_revised
elif not new_character.isalpha() and i.isalpha() and i.islower():
if new_index_number > 122:
new_character_revised = index_higher_122(shift, i)
ciphertext += new_character_revised
else:
ciphertext += new_character
else:
ciphertext += i
print(ciphertext)
def decrypt():
pass
if __name__ == '__main__':
user_choice = (input("Encrypt or Decrypt "))
if user_choice == "Encrypt" or "encrypt":
encrypt()
I tried to make changes to the index_higher_90 and index_higher_122 to make sure the numbers were right and it wasn't a problem with the math, but that didn't help either.