Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# PRODIGY_CS_01

# Caesar Cipher 🔐
Expand Down
60 changes: 44 additions & 16 deletions caesar_cipher.py
Original file line number Diff line number Diff line change
@@ -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!")
if __name__ == "__main__":
main()