From be057c150cba5f4f6630026c7326ccce6880345b Mon Sep 17 00:00:00 2001 From: Daniel VanKirk Date: Wed, 13 Nov 2024 21:08:13 -0500 Subject: [PATCH] updated assignment --- cipher.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/cipher.py b/cipher.py index 0e772db..a9e0109 100644 --- a/cipher.py +++ b/cipher.py @@ -1 +1,26 @@ -# add your code here +def caesar_cipher(text, shift, mode="encrypt"): + result = "" + if mode == "decrypt": + shift = -shift + + for char in text: + if char.isupper(): + shifted_char = chr((ord(char) - 65 + shift) % 26 + 65) + result += shifted_char + elif char.islower(): + shifted_char = chr((ord(char) - 97 + shift) % 26 + 97) + result += shifted_char + else: + result += char + + return result + +text = input("Enter the text: ") +shift = int(5) +mode = input("Enter mode (encrypt/decrypt): ").strip().lower() + +if mode in ["encrypt", "decrypt"]: + output = caesar_cipher(text, shift, mode) + print(f"Result: {output}") +else: + print("Invalid mode selected. Please choose 'encrypt' or 'decrypt'.") \ No newline at end of file