# CS21 Lab3: If/else statements

Due Saturday (February 14) before midnight

This lab assignment requires you to write two 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 directory, so make sure your programs are in this directory! Programming tips As you write your first programs, start using good programming practices now: • Use a comment at the top of the file to describe the purpose of the program. • Use variable names that describe the contents of the variables. • Use def main() to combine the steps of your program into a function. • Write your programs incrementally and test them as you go. This is really crucial to success: test, test again, and then test some more! Don't assume that if your program passes the sample tests we provide that it is completely correct. Come up with your own test cases and verify that the program is producing the right output on them. 1. Fantasy Football Kicker Calculator Write a program that computes the number of points a kicker would earn in fantasy football. Points are awarded according to the following rules: • Each Field Goal Made (50+ yards) = 5 points • Each Field Goal Made (40-49 yards) = 4 points • Each Field Goal Made (0-39 yards) = 3 points • Each Extra Point Attempt = 1 point • Each Field Goal Missed = -1 point (subtract a point from total) Your program should first prompt the user to enter the total number of field goals made for the week. It should then ask how many yards each field goal was (with the field goal number in the prompt). After that, ask for the number of extra points that were made and how many field goals were missed. You can assume that the user will enter integers for all responses. Use the information provided to calculate how many fantasy football points the kicker has earned for this week. Use the guideline below to print out a kicker a rating for the week.  less than 3 points mediocre 3-6 points typical 7-10 points good more than 10 points great Here are some sample runs showing how your program should work: $ python kicker.py
How many field goals were made this week? 3
How many yards was field goal number 1? 30
How many yards was field goal number 2? 40
How many yards was field goal number 3? 50
How many extra points were made this week? 2
How many field goals were missed this week? 1

Your kicker earned 13 points and had a great week.

$python kicker.py How many field goals were made this week? 2 How many yards was field goal number 1? 25 How many yards was field goal number 2? 39 How many extra points were made this week? 1 How many field goals were missed this week? 2 Your kicker earned 5 points and had a typical week.  $ python kicker.py
How many field goals were made this week? 0
How many extra points were made this week? 1
How many field goals were missed this week? 0

Your kicker earned 1 point and had a mediocre week.

2. Secret Messages
Valentines day is coming and you would like a way to send a note to someone special, but you are worried that it might be intercepted and fall into the wrong hands. So you decide to encrypt it using a form of encryption known as a transposition cipher. This works by transposing letters in the message with a certain pattern.

Let's see an example of how this encryption works by encrypting the phrase "The fat cat." The first thing our encryption algorithm does is strip out everything that is not a character. This gives us the string "Thefatcat". Now we break up the string into rows and columns. For this example we are choosing the column size of three. When we do this our string now looks like:

The
fat
cat

The encrypted text is the phrase you get reading reading down from the columns:
Tfc haa ett

In this example, "Tfc" is the first column, "haa" is the second column, and "ett" is the third column. Let's look at another with the phrase "meet me in sharples at six o'clock" and a column size of 5. First we remove anything that is not a letter from our string to get "meetmeinsharplesatsixoclock". Then we block off the text in groups of 5.
meetm
einsh
arple
satsi
xoclo
ck


In this case, the string doesn't fill out the block completely, so we need to pad out the string with extra characters. For this assignment, we will pad the string out with the lowercase 'x's. This give us:

meetm
einsh
arple
satsi
xoclo
ckxxx

Now we read down the columns to get our encrypted string: "measxceiraokenptcxtslslxmheiox" (column 1: measxc, column 2: eiraok column 3: enptcx, column 4: tslslx, and column 5: mheiox). To make the encrypted string easier to read, we'll always display our message in groups of 5 characters (this is the traditional way to print out encrypted text messages).
measx ceira okenp tcxts lslxm heiox


To complete this assignment, your program should do the following:

1. Prompt the users for a text message to encrypt.
2. Ask the user for the column size to encrypt the message with.
3. Remove everything that isn't the characters 'A' through 'Z' and 'a' through 'z' (this means removing numbers,spaces, and all other symbols)
4. If the string isn't an even multiple of the column size, pad out the string (by adding one or more of the letter 'x') to the end of the string to make it a multiple of the column size.
5. Encrypt the string by taking all the characters in the first column, second column, etc.
6. Print the encrypted string in groups of 5 to the user.

Here are some examples of the program being run:

$python cipher.py Enter the message to encrypt: meet me in sharples at six o'clock. How many columns to use in encryption? 5 Your encrypted message is: measx ceira okenp tcxts lslxm heiox  $ python cipher.py
Enter the message to encrypt: I think therefore I am.
How many columns to use in encryption? 3

Your encrypted message is:
Iitro Itnhe rahke fem

$python cipher.py Enter the message to encrypt: Live life to the fullest. How many columns to use in encryption? 5 Your encrypted message is: Litli fhlve eeetf slout  $ python cipher.py
Enter the message to encrypt: Don't Panic
How many columns to use in encryption? 3

Your encrypted message is:
DtnoP inac

$python cipher.py Enter the message to encrypt: So long, and thanks for all the fish. How many columns to use in encryption? 6 Your encrypted message is: Sanaf onkli ldsls otfth nhohx garex  $ python cipher.py
Enter the message to encrypt: Time is an illusion. Lunchtime doubly so.
How many columns to use in encryption? 4

Your encrypted message is:
TiisL heboi sliut dlxma lonio yxenu ncmus x

Hints for doing this assignment:
• Do one step at a time and test each step before you move on to the next one. For example, implement and test the code which removes spaces and symbols before moving on to the part which adds padding to the string.
• When testing your program, comment out the part of your code that asks the user for a string, and assign that variable a string from one of the examples above. This way when testing your program you don't have to enter the same string over and over again while getting your program to work. But remember to put it back to prompting for input before you submit your assignment for grading.
• Pulling out the right characters from the string for the encryption can be tricky. The list slicing operator has an extra option, a step parameter, that can save you a lot of time and effort on this assignment. For example, if you have lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], lst[0:8] will give you a list with the numbers 0 through 7. The slicing operator can take a third parameter, that goes after the second ':' and is a step size. It works like the step parameter for the range operator. For example, lst[0:8:2] gives you a list of: [0, 2, 4, 6], and lst[0:8:3] gives you a list of [0, 3, 6]. This also works for strings, and if the start and end parameters are not given, the default is the start and end of your list or string. For example, lst[::2] gives [0, 2, 4, 6, 8].
Hacker's challenge
This is an optional bonus exercise. It is challenging and is not required, and you should not attempt it until you are completely finished with the other lab exercises. It will also not get you any extra points -- you will only be rewarded with extra knowledge and satisfaction. :)

Write a program, decrypt.py to decrypt the output from the previous program. The user will have to enter the encrypted string, and the number of columns used to encrypt the phrase.

Submit

Remember you may run handin21 as many times as you like. Each time you run it new versions of your files will be submitted. Running handin21 after you finish a program, after any major changes are made, and at the end of the day (before you log out) is a good habit to get into.