CS21 Lab 3: if/elif/else

Due before Midnight Saturday 28 September 2013

This lab assignment requires you to write three programs in python. First, run update21. This will create the cs21/labs/03 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/03 directory and begin working on the python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

$ update21
$ cd cs21/labs/03
$ pwd
/home/your_user_name/cs21/labs/03
We will only grade files submitted by handin21 in this labs directory, so make sure your programs are in this directory!

1. Rate your quarterback

Write a program called quarterback.py that prompts the user for a football quarterback's stats (passing attempts, completions, total passing yards, touchdowns, and interceptions) and calculates the quarterbacks rating according to the following formula:

ATT = Number of passing attempts
COMP = Number of completions
TD = Touchdown passes
INT = Interceptions
YARDS = Total passing yards

QBR = ((8.4 * YARDS) + (330 * TD) + (100 * COMP) - (200 * INT)) / ATT

Your program should calculate the QBR and then output a rating based on the following table (a higher QBR is better):

Rating QBR
Excellent >= 175
Great < 175 and >= 150
Above Average < 150 and >= 125
Average < 125 and >= 100
Below Average < 100 and >= 75
Poor < 75

Here are two sample runs of such a program:

$ python quarterback.py

Total pass attempts: 40
Completions: 33
Touchdowns: 4
Interceptions: 0
Total yards: 670

QBR = 256.2 (Excellent)

$ python quarterback.py

Total pass attempts: 44
Completions: 26
Touchdowns: 5
Interceptions: 1
Total yards: 388

QBR = 166.1 (Great)

Here are some quarterbacks to test your code against.

2. Encrypted message

A simple way to send encrypted messages is with a substitution cipher, where one character in a message is replaced by another letter.

letters = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
cipher  = list("JILSPENXKMWUCVTDYHBROZFGAQ")
For the cipher above, to encode the word "HELLO" you would substitute the letter 'H' with the letter 'X; the letter 'E' with the letter 'P'; the letter 'L' with the letter 'U'; and the letter 'O' with the letter 'T', giving you the secret word 'XPUUT'. You will write a program called decode.py which will ask for a message encoded by this cipher and decode it for you. You can assume your input message will only have uppercase letters [A-Z] and spaces. You do not have to worry about decoding any other characters.

Note: there are many advanced ways you could do this in python. For this lab, please stick with what we have learned so far: for loops, accumulators, if/else, etc.

Here are two sample runs of such a program:

 
$ python decode.py
Enter secret message: XPUUT
Your message is: HELLO

$ python decode.py
Enter secret message: IP BOHP RT SHKVW ATOH TZJURKVP
Your message is: BE SURE TO DRINK YOUR OVALTINE

Here are some more secret strings with their decoded output to test against:

 
RXP YOKLW IHTFV ETG MOCDPS TZPH RXP UJQA STN
THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG

RXKB KB J RPBR LKDXPH
THIS IS A TEST CIPHER

RXP PJNUP EUKPB JR SJFV
THE EAGLE FLIES AT DAWN

LJV ATO LHJLW RXP LTSP
CAN YOU CRACK THE CODE

2b. Hacker's Challenge: message encoding

This problem is an optional bonus problem. It is not required, and you should not attempt it until you are completely finished with the other three lab problems. It will also not get you any extra points -- you will only be rewarded with extra knowledge and satisfaction. :)

Write a program called encode.py which will ask for a message and then encode it using the cipher from the previous program.

 
$ python encode.py
Enter your message: THIS IS A TEST MESSAGE
Your encrypted message is: RXKB KB J RPBR CPBBJNP

3. Eggy-Peggy

Eggy-peggy is a secret language which is used mostly by school children in England. To write an eggy-peggy sentence, you add an "egg" before each vowel. For example, "Mary had a little lamb." would go to: "Meggary heggad egga leggittlegge leggamb."

You will write a program eggy.py which will ask the user for a sentence and then print out that sentence in eggy-peggy.

Here are two sample runs of such a program:

 
$ python eggy.py
Enter your message: Mary had a little lamb.
Your message in eggy-peggy is: Meggary heggad egga leggittlegge leggamb.

$ python eggy.py
Enter your message: CS21 is great!
Your message in eggy-peggy is: CS21 eggis greggeeggat!

Note: one tricky aspect is handling words that have capital vowels in them. In this case, you should add an 'Egg' in front of the vowel and the vowel should become lower case. For example, 'April' would become 'Eggapreggil'. You may find the string library function lower() to be useful. It takes a string and returns the lowercase version of that string. Remember you need to first import the string library to use this function. Here is an example of how this function works:

 
>>> from string import *
>>> ch = lower("S")
>>> print ch
s
>>> ch = lower("H")
>>> print ch
h
>>> ch = lower("Hello")
>>> print ch
hello
>>> 


Submit

Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt.

You may run handin21 as many times as you like. We will grade the most recent submission submitted prior to the deadline

Remember: for this lab, programs must appear in your cs21/labs/03 directory. If you create your programs in a different directory, use the unix mv or cp commands to move or copy them into the cs21/labs/03 directory. For example:

 cp  myprog.py  ~/cs21/labs/03/myprog.py