CS303E Assignment 6

Due Wednesday, March 20th by 11pm

For this assignment, you will write a program that implements a Caesar Cipher. A Caesar Cipher is a simple encryption method that encrypts a message by shifting each character either forward or backward a fixed number of positions in the alphabet. You may work in pairs for this assignment, but you must follow the guidelines covered in Pair Programming Guidelines. If you work in a pair, you will receive two extra credit points on your grade for this assignment. You must partner with someone in your lecture.

Note: You may not use any programming construct that we have not covered in class. Functions from the string library are allowed.

File Name: Cipher.py

Your program should prompt the user for the message to be encoded, a key value (the number of positions to shift the characters), and the format of the final message. For instance, the letter "a" with a key value of 4 would result in the letter "e", becuase "e" is four places forward in the alphabet from "a". The format of the final message will be either lower case, upper case, or as is.

Once it has gathered the information from the user, your program should convert the message to the appropriate format and then call the function encrypt_msg().

encrypt_msg() is a function written by you that accepts two parameters: the message and the key_value. It should encrypt the message appropriately and then print it to the screen. Three things to note about this function:

  1. the encryption should be circular, so that the next character after "z" should be "a" and the next character after "Z" should be "A". (Hint: consider using modulus.)
  2. non-letter characters should remain the same (so don't use this to encrypt a phone number!)
  3. the encryption required here is not the same as the encryption mentioned in your textbook, so the assignment will be much easier if you do not try to use chr() or ord().

A final hint:
Recall the string.find function.

  
string.find(source, sub) 
Return the lowest index in source where the substring sub is found such that sub is wholly contained in source. Return -1 on if sub is not present in source.

So for example:


lyric = 'EASY_AS_ABC'
print string.find(lyric, 'E') # prints 0
print string.find(lyric, 'A') # prints 1
print string.find(lyric, 'B') # prints 9
print string.find(lyric, '!') # prints -1

Here are some sample runs of the program with the user's inputs in green:


Please enter a string to be decoded: Mary had a little lamb
Please enter the key value: 5
Should the encoded message be in lower case, upper case, or as is?
Please enter "lower", "upper", or "as is": upper
RFWD MFI F QNYYQJ QFRG

Please enter a string to be decoded: Mary had a little lamb
Please enter the key value: 8
Should the encoded message be in lower case, upper case, or as is?
Please enter "lower", "upper", or "as is": as is
Uizg pil i tqbbtm tiuj

Please enter a string to be decoded: Hello!  How are you?
Please enter the key value: 40
Should the encoded message be in lower case, upper case, or as is?
Please enter "lower", "upper", or "as is": as is
Vszzc!  Vck ofs mci?

Please enter a string to be decoded: Vszzc!  Vck ofs mci?
Please enter the key value: -40
Should the encoded message be in lower case, upper case, or as is?
Please enter "lower", "upper", or "as is": lower
hello!  how are you?

Please enter a string to be decoded: Baa Baa Black Sheep
Please enter the key value: 7
Should the encoded message be in lower case, upper case, or as is?
Please enter "lower", "upper", or "as is": blah
Unrecognized format.  Leaving message as is.
Ihh Ihh Ishjr Zollw

Test your program by running it several times. Put your test runs and their output in comments at the end of your .py file

Name your file Cipher.py. Be certain to begin your file with the following header:


# File: --name of file--
# Description: --a description of your program--
# Assignment Number:
#
# Name: --your name--
# EID: --your eid--
# Course Name: CS303E
#
# Unique Number: --your section number--
#
# Date created:
# Date last modified:
#
# Slip days used this assignment:
# Total slip days used:
If you used pair programming, include name, EID, course name, and unique number for both partners along with the other required information.

Use the turnin program to submit your file. The file must be turned in by March 20th at 11pm. If you use slip days, please notify your grader via email when you turn in your file. 

If you used pair programming, both partners must have enough slip days left to cover any slip days you use. (e.g., If you use two, both partners must have at least two left to use.) Turn the assignment in using just one partner's turnin account. The grader will grade it and enter the grade for both partners.

Did you remember to: