1

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.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 1
    (btw your conditional: `user_choice == "Encrypt" or "encrypt"` doesn't do what you think it does. See the answers to this [question](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al) ) – quamrana Jun 19 '23 at 16:22
  • It would help make a [mcve] if you would [edit] your question to include a sample of the input you are using, and the current 'wrong' output vs the expected output based on that input – G. Anderson Jun 19 '23 at 16:28
  • 2
    If you shift an uppercase letter by certain values, it will become a lower case letter, and your test to see if you need to wrap around will fail. Ex: `'Z' + 7 = 'a'` and `not new_character.isalpha()` fails. – 001 Jun 19 '23 at 16:39
  • `and i.isalpha()` is redundant, since you're inside `if is.isalpha()` – Barmar Jun 19 '23 at 16:46
  • Thanks 001, This was the main problem in the code. – The one above all Jun 21 '23 at 17:52

0 Answers0