A Python project where you'll build a secret message encoder using the Caesar Cipher — one of the oldest encryption techniques in history!
A Caesar Cipher works by shifting every letter in a message by a fixed number of positions in the alphabet. For example, with a shift of 3:
A → D
B → E
C → F
...
Z → C (wraps back around!)
So the message HELLO becomes KHOOR with a shift of 3.
Write a Python program that:
- Asks the user for a message and a shift number
- Loops through every character in the message
- Shifts each letter by the given amount
- Leaves spaces, numbers, and punctuation unchanged
- Prints the encoded message
Before you start coding, read up on these three built-in Python functions — they're the key to making your cipher work!
Converts a character to its ASCII number (its position in the computer's character table).
📖 Read the W3Schools docs on ord()
ord('A') # returns 65
ord('a') # returns 97Does the opposite — converts an ASCII number back into a character.
📖 Read the W3Schools docs on chr()
chr(65) # returns 'A'
chr(97) # returns 'a'Checks whether a character is a letter (and not a space, number, or symbol). Returns True or False.
📖 Read the W3Schools docs on isalpha()
'A'.isalpha() # returns True
'3'.isalpha() # returns False
' '.isalpha() # returns Falsemessage = input("Enter your message: ")
shift = int(input("Enter the shift number: "))encoded = ""Use a for loop to go through the message one character at a time:
for char in message:
# your encoding logic goes hereInside your loop, use .isalpha() to decide what to do:
- If it is a letter → shift it
- If it isn't a letter → leave it alone and add it as-is
This is the tricky part! Here's the idea:
- Use
ord()to get the character's ASCII number - Add the shift value
- Use
chr()to convert the number back to a character
Watch out for wrapping! After
Z(ASCII 90) you need to wrap back around toA(ASCII 65). The%(modulo) operator can help with this.Hint: Uppercase letters run from 65–90, and lowercase letters run from 97–122. You'll want to handle both cases!
print("Encoded message:", encoded)Once your basic encoder works, try adding these features (hint some of them we will be doing later in the project):
- Decoder — Add an option to decode a message (hint: shift in the opposite direction)
- Brute Force — Print all 26 possible shifts so you can crack a cipher without knowing the key
- File I/O — Read a message from a
.txtfile and write the encoded version to a new file
Enter your message: Hello, World!
Enter the shift number: 3
Encoded message: Khoor, Zruog!
- Test with a shift of 0 — the message should not change
- Test with a shift of 26 — the message should also not change (full wrap)
- Make sure spaces and punctuation stay in place
- Try encoding a message with one program and decoding it with another!
Good luck, and keep your messages secret! 🕵️