Skip to the content.

SafeComputing

homework/popcorn hacks

Homework Hack

import random

def caesar_cipher(text, shift, mode):
    result = ""
    for char in text:
        if char.isalpha():
            shift_amount = shift if mode == "encrypt" else -shift
            new_char = chr(((ord(char.lower()) - 97 + shift_amount) % 26) + 97)
            result += new_char.upper() if char.isupper() else new_char
        else:
            result += char
    return result

# User input
mode = input("Hi! Would you like to encrypt or decrypt a message today? ").strip().lower()
message = input("Enter message here: ")
shift_input = input(" enter a number of places to shift or type 'random' and have the computer choose for you!: ").strip().lower()

# Handle random shift
if shift_input == "random":
    shift = random.randint(1, 25)
    print(f"Random shift selected: {shift}")
else:
    shift = int(shift_input)

# Perform encryption/decryption
output = caesar_cipher(message, shift, mode)
print(f"Result: {output}")

Result: xjjfdzn