*Project :- *
Caesar Cipher Program
Import and display the logo from the art file.
Define the alphabet as a list of lowercase English letters.
Define a function for the Caesar cipher which takes:
The original text,
A shift amount,
The direction (encode or decode).
Initialize an empty output text.
For each character in the original text:
If the character is not in the alphabet:
Add it to the output text without change.
Else:
If the direction is "decode", multiply the shift by -1.
Find the character’s index in the alphabet and add the shift amount (use modulo length of alphabet to wrap).
Add the shifted character to the output text.
Print whether the result is encoded or decoded.
Start a loop that will continue running until the user chooses to exit:
Ask the user if they want to encode or decode.
Ask the user for the message.
Ask the user for the shift number.
Call the Caesar cipher function using the input.
Ask the user if they want to restart or exit.
If they choose "no," exit and print "Goodbye!"
This captures input validation, letter shifting, user choice, repeating functionality, and correct stopping.
CREATION (hands-on) — Case-preserving Caesar + Brute-force decode
Goal (real-world / logic building):
Improve your cipher so it (A) preserves letter case (uppercase letters stay uppercase, lowercase stay lowercase), (B) accepts any shift number (e.g., 29 → same as 3), and (C) adds a “brute-force decode” mode that shows all possible 26 decodings for the user to choose from when they don’t know the shift.
Requirements
Keep your caesar() function but modify it to preserve case:
If input letter is uppercase 'A'..'Z', shift within uppercase alphabet.
If lowercase, shift within 'a'..'z'.
Non-letters (spaces, punctuation, digits) remain unchanged (as you already do).
Normalize shift_amount by doing shift_amount = shift_amount % 26 so large shifts work.
Add support in main():
If user types direction == "bruteforce" (instead of 'encode'/'decode'), call the caesar() function multiple times to print all 26 possible decryptions (or 25 if you prefer) for the given text so user can visually pick the correct one.
Keep the same user interface: ask direction, text, and shift (shift ignored for bruteforce but ask it or set to 0).
Do not use any advanced libraries — only loops, conditionals, and list/index operations.
Sample interaction
Type 'encode', 'decode' or 'bruteforce': bruteforce
Type your message:
Uifsf jt b tfdsfu nfttbhf!
Type the shift number (enter 0 to ignore):
0
Shift 1: There is a secret message!
Shift 2: Sgdqd hr z rdbqds ldrrzfd!
...
Shift 25: Vju...
DEBUGGING (short snippet) — Repeated shift negation (subtle bug)
Buggy snippet (very short — this is inside your caesar() function):
for letter in original_text:
if letter not in alphabet:
output_text += letter
else:
if encode_or_decode == "decode":
shift_amount *= -1 # <-- BUG: this changes shift_amount on every letter!
shifted_position = alphabet.index(letter) + shift_amount
shifted_position %= len(alphabet)
output_text += alphabet[shifted_position]
How to reproduce
Input direction = "decode", text = "bbb", shift = 1.
Expected decode with shift 1: "aaa".
Actual output with this buggy code: it will alternate or behave incorrectly (because shift_amount flips sign on each character).
What’s wrong (concise)
shift_amount *= -1 is run inside the per-letter loop, so the sign (and even magnitude if coded differently) is changed for every character. That makes subsequent characters use the wrong shift. The sign flip should be applied once for the whole call, not per character.
Your task (do not ask for the fixed code)
Move the decode sign change so it executes once before the for loop.
Ensure the shift is normalized (e.g., shift_amount = shift_amount % 26) before any negation or use.
After your fix, show that:
direction = "decode", text = "bbb", shift = 1 → prints "aaa".
direction = "encode", text = "aZ!", shift = 2 → prints "cB!" (case preserved if you implemented creation).
Hint (single line): flip the sign of shift_amount once outside the loop, and use % 26 so large shift values behave correctly.
Goal
Fix the bug so decoding uses the correct constant negative shift for the entire message.
Thank you for completing Day 07
Top comments (0)