Python Forum
Homework: Ceaser Cipher encoder and decoder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework: Ceaser Cipher encoder and decoder
#1
I am new to python and can't seem to get the 3rd input line to work with the "method" if loops. How would I get them to acknowledge the string "encrypt" or "decrypt"? When I run the program it takes the input but will still run through my second "method" input.
message = input("Enter the message you wish to encrypt or decrypt:")
shift_key = int(input("Enter the key value:"))
method = str(input("Would you like to encrypt or decrypt a message?"))
if method != str("encrypt") or method != str("decrypt"):
    method = input("Enter either encrypt or decrypt")
else:
    if method == str("encrypt"):
        encrypted_message = ""
        for character in message:
            if character.isalpha() == True:
                if character == character.lower():
                    x = ord(character) -97
                    x += int(shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 97)
                else:
                    x = ord(character) -65
                    x += int(shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 65)
            else:
                encrypted_message += character
                print(encrypted_message)
    else method == str("decrypt"):
        decrypted_message = ""
        for character in message:
            if character.isalpha() == True:
                if character == character.lower():
                    x = ord(character) -97
                    x += int(-shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 97)
                else:
                    x = ord(character) -65
                    x += int(-shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 65)
            else:
                decrypted_message += character
                print(decrypted_message)
Reply
#2
Hello and welcome to Python and the forums!
This program cannot run because there is a syntax error on line #24.
If you want to have another condition check after "if", you need to use "elif". In "else" you don't put any conditon and it is usef for any other case, not previously checked/handled. You can refer this tutorial page.
Reply
#3
Ok, I fixed the error on line 24 but i'm still getting the request for input from line 5. I am looking for line 4 to "see" the input from line 3 and skip past line 4 and 5.

message = input("Enter the message you wish to encrypt or decrypt:")
shift_key = int(input("Enter the key value:"))
method = str(input("Would you like to encrypt or decrypt a message?"))
if method != str("encrypt") or method != str("decrypt"):
    method = input("Enter either encrypt or decrypt")
else:
    if method == str("encrypt"):
        encrypted_message = ""
        for character in message:
            if character.isalpha() == True:
                if character == character.lower():
                    x = ord(character) -97
                    x += int(shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 97)
                else:
                    x = ord(character) -65
                    x += int(shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 65)
            else:
                encrypted_message += character
                print(encrypted_message)
    elif method == str("decrypt"):
        decrypted_message = ""
        for character in message:
            if character.isalpha() == True:
                if character == character.lower():
                    x = ord(character) -97
                    x += int(-shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 97)
                else:
                    x = ord(character) -65
                    x += int(-shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 65)
            else:
                decrypted_message += character
                print(decrypted_message)
Reply
#4
Your logical operator on line 4 is wrong. It should be:

if method != str("encrypt") and method != str("decrypt"):
If you're using an "or" you end up in the following problem:

Do you want to encrypt or decrypt?
I want to encrypt.
Does encrypt not equal encrypt? False! Encrypt does equal encrypt.
Does encrypt not equal decrypt? True! Encrypt does not equal decrypt.
False or true, therefore true.
Reply
#5
Thank you, that fixed that issue and it makes sense, which is the most important thing. Now I seem to be getting an unexpected output. When I use the message "This is so cool" and a shift value of "3" I am getting 3 output lines consisting of "Wklv" "Wklv lv" and "Wklv lv vr". I have no idea where i'm going wrong here.
message = input("Enter the message you wish to encrypt or decrypt:")
shift_key = int(input("Enter the key value:"))
method = str(input("Would you like to encrypt or decrypt a message?"))
if method != str("encrypt") and method != str("decrypt"):
    method = input("Enter either encrypt or decrypt")
else:
    if method == str("encrypt"):
        encrypted_message = ""
        for character in message:
            if character.isalpha() == True:
                if character == character.lower():
                    x = ord(character) -97
                    x += int(shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 97)
                else:
                    x = ord(character) -65
                    x += int(shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 65)
            else:
                encrypted_message += character
                print(encrypted_message)
    elif method == str("decrypt"):
        decrypted_message = ""
        for character in message:
            if character.isalpha() == True:
                if character == character.lower():
                    x = ord(character) -97
                    x += int(-shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 97)
                else:
                    x = ord(character) -65
                    x += int(-shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 65)
            else:
                decrypted_message += character
                print(decrypted_message)
Reply
#6
Line 10 is the culprit. The words are properly encoded as they're coming out. However, your encryption only prints when the character is not an alpha character. So, it prints the first encrypted word because the code reached the space, which is not an alpha. Then it does it again for the second encrypted word and the third. It never prints the whole thing because it is never told to do so.
Reply
#7
How can I go about fixing it? Should I back up my print statement to fall under my for loop?
Reply
#8
You can move it left so it's at the same level as line 9. That way, your loop will complete without printing anything and then it will print the final, encrypted message.
Reply
#9
Success!!! Thank you for the help. It was nice to see I wasn't too far from being correct. This is the most intricate program I've written so far. [edited] Ok, got a little ahead of myself. I tried the decrypter portion of the program and got a traceback error of
Error:
encrypted_message += chr(x + 65) NameError: name 'encrypted_message' is not defined
should I remove the method == str("decrypt") and change the elif back to an else?

message = input("Enter the message you wish to encrypt or decrypt:")
shift_key = int(input("Enter the key value:"))
method = str(input("Would you like to encrypt or decrypt a message?"))
if method != str("encrypt") and method != str("decrypt"):
    method = input("Enter either encrypt or decrypt")
else:
    if method == str("encrypt"):
        encrypted_message = ""
        for character in message:
            if character.isalpha() == True:
                if character == character.lower():
                    x = ord(character) -97
                    x += int(shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 97)
                else:
                    x = ord(character) -65
                    x += int(shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 65)
            else:
                encrypted_message += character
        print(encrypted_message)
    elif method == str("decrypt"):
        decrypted_message = ""
        for character in message:
            if character.isalpha() == True:
                if character == character.lower():
                    x = ord(character) -97
                    x += int(-shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 97)
                else:
                    x = ord(character) -65
                    x += int(-shift_key)
                    x = x % 26
                    encrypted_message += chr(x + 65)
            else:
                decrypted_message += character
        print(decrypted_message)
Reply
#10
No, the problem is on lines 31 and 37. In that part of the code, encrypted_message doesn't exist. You need decrypted_message instead.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Ceaser cipher program oli_action 5 2,878 Sep-14-2020, 10:43 AM
Last Post: oli_action
  Cipher Caesar Azilkhan 1 2,148 Nov-21-2019, 03:40 PM
Last Post: ichabod801
  Monoalphabetic cipher pawlo392 1 12,803 Apr-01-2019, 08:51 PM
Last Post: ichabod801
  Caesar cipher Drone4four 19 10,768 Nov-11-2018, 04:07 AM
Last Post: nilamo
  Playfair cipher encoder and decoder nuttinserious 1 5,926 Oct-23-2017, 06:41 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020