CS 111
Fall 2016
Introduction to Computer Science
HW04: Caesar cipher
Due: Monday, 09/26 at 23:55
The Caesar cipher is a very simple way of creating coded messages. The original cipher, attributed to Julius Caesar, involves shifting every letter in a plaintext message 3 positions further down in the alphabet to get the corresponding ciphertext message. For example, a Q in the plaintext message would be shifted by three postions to become a T in the ciphertext. A plaintext message of "YAK AND ZEBRA" would be transformed to the ciphertext message "BDN DQG CHEUD". Note that letters near the end of the alphabet wrap around to the beginning (e.g., Y + 3 = B). You can, of course, perform a Caesar cipher transformation using other values than 3 for the shift. "YAK AND ZEBRA" with a shift of 4, for example, becomes "CEO ERH DIFVE".
In this assignment, you will write a function that implements the Caesar cipher. This will give you some practice encapsulating a collection of operations into a Python function.
Your task
Write a program that:
- Asks the user for an integer (the Caesar shift value) and a string (the plaintext),
- Calls a function named
caesar
(see below for details) to obtain the ciphertext, and - Prints the ciphertext.
Your caesar
function should look like this:
A typical session with your completed program might look like this:
prompt$ python3 caesar.py What is your plaintext? follow the elk What shift do you want to use? 3 Your ciphertext is: iroorz wkh hon prompt$
A little help
- To step one character at a time through a string, see the strings2.py sample from last Friday's lab.
- To shift a letter to another letter by some integer amount, you
will need to use the Python functions
ord
andchr
, which are described in Chapter 5 of your textbook, starting on page 131.
Start early, have fun, and ask questions.