diff --git a/README.md b/README.md index 9207381..26382c9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # PRODIGY_CS_01 # Caesar Cipher 🔐 diff --git a/caesar_cipher.py b/caesar_cipher.py index 96e10f9..e81a4e2 100644 --- a/caesar_cipher.py +++ b/caesar_cipher.py @@ -1,34 +1,62 @@ + + alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] - def caesar(original_text, shift_amount, encode_or_decode): output_text = "" if encode_or_decode == "decode": shift_amount *= -1 + elif encode_or_decode != "encode": + print("Invalid direction. Please use 'encode' or 'decode'.") + return - for letter in original_text: + shift_amount = shift_amount % len(alphabet) # Handle large shifts - if letter not in alphabet: + for letter in original_text: + if letter.lower() not in alphabet: output_text += letter else: - shifted_position = alphabet.index(letter) + shift_amount + # Preserve original case + is_upper = letter.isupper() + letter_lower = letter.lower() + + shifted_position = alphabet.index(letter_lower) + shift_amount shifted_position %= len(alphabet) - output_text += alphabet[shifted_position] - print(f"Here is the {encode_or_decode}d result: {output_text}") + new_letter = alphabet[shifted_position] + + output_text += new_letter.upper() if is_upper else new_letter + + print(f"\nHere is the {encode_or_decode}d result: {output_text}\n") + +def get_valid_shift(): + while True: + try: + shift = int(input("Type the shift number (0-25 recommended): ")) + return shift + except ValueError: + print("Please enter a valid integer.") +def main(): + to_continue = True + print("Welcome to the Caesar Cipher Program!") -to_continue = True + while to_continue: + print("\n" + "="*40) + direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n").lower() + if direction not in ['encode', 'decode']: + print("Invalid choice. Please try again.") + continue -while to_continue: + text = input("Type your message:\n") + shift = get_valid_shift() - direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n").lower() - text = input("Type your message:\n").lower() - shift = int(input("Type the shift number:\n")) + caesar(text, shift, direction) - caesar(text,shift,direction) + restart = input("\nType 'yes' if you want to go again. Otherwise, type 'no':\n").lower() + if restart != 'yes': + to_continue = False + print("\nThank you for using the Caesar Cipher Program!") - restart = input("Type 'yes' if you want to go again. Otherwise, type 'no'.\n").lower() - if restart == "no": - to_continue = False - print("Thank You!") \ No newline at end of file +if __name__ == "__main__": + main()