Homework: Ceaser Cipher encoder and decoder - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Homework: Ceaser Cipher encoder and decoder (/thread-13700.html) Pages:
1
2
|
Homework: Ceaser Cipher encoder and decoder - Siylo - Oct-27-2018 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) RE: Homework: Ceaser Cipher encoder and decoder - j.crater - Oct-27-2018 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. RE: Homework: Ceaser Cipher encoder and decoder - Siylo - Oct-27-2018 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) RE: Homework: Ceaser Cipher encoder and decoder - stullis - Oct-27-2018 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. RE: Homework: Ceaser Cipher encoder and decoder - Siylo - Oct-27-2018 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) RE: Homework: Ceaser Cipher encoder and decoder - stullis - Oct-27-2018 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. RE: Homework: Ceaser Cipher encoder and decoder - Siylo - Oct-27-2018 How can I go about fixing it? Should I back up my print statement to fall under my for loop? RE: Homework: Ceaser Cipher encoder and decoder - stullis - Oct-27-2018 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. RE: Homework: Ceaser Cipher encoder and decoder - Siylo - Oct-27-2018 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 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) RE: Homework: Ceaser Cipher encoder and decoder - stullis - Oct-27-2018 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. |