importrandomdefcaesar_cipher(text,shift,mode):result=""forcharintext:ifchar.isalpha():# Encrypts only letters
shift_amount=shiftifmode=="encrypt"else-shiftnew_char=chr(((ord(char.lower())-97+shift_amount)%26)+97)result+=new_char.upper()ifchar.isupper()elsenew_charelse:result+=char# Keeps spaces and punctuation unchanged
returnresult# Getting user input
mode=input("Do you want to encrypt or decrypt? ").strip().lower()message=input("Enter your message: ")shift_input=input("Enter shift value (number of places to shift) or type 'random': ").strip().lower()# Handling the "random" shift value
ifshift_input=="random":shift=random.randint(1,25)print(f"Random shift value selected: {shift}")else:shift=int(shift_input)# Performing encryption/decryption
output=caesar_cipher(message,shift,mode)print(f"Result: {output}")