CS21 Lab 3: if/elif/else

Due 11:59pm Fri, Feb 15th

Run update21, if you haven't already, to create the cs21/labs/03 directory and any starter files. Then cd into your cs21/labs/03 directory and create the python programs for lab 3 in this directory (handin21 looks for your lab 3 assignments in your cs21/labs/03 directory):

$ update21
$ cd
$ cd cs21/labs/03
$ pwd

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page.

1. baseball statistics

Write a program called whip.py that asks the user for a baseball pitcher's stats (walks, hits, innings pitched) and the calculates the pitcher's WHIP (Walks plus Hits per Innings Pitched):

WHIP = (Walks + Hits)/(Innings Pitched)

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

Rating WHIP
Excellent <= 1.00
Great 1.00 - 1.10
Above Average 1.10 - 1.25
Average 1.25 - 1.32
Below Average 1.32 - 1.40
Poor 1.40 - 1.50
Awful > 1.50

Here's an example of this program, using data from Cole Hamels last year:

$ python whip.py

  walks: 52
   hits: 190
innings: 215

WHIP = 1.13 (Above Average)

2. simplified scrabble score

Suppose you have two parallel lists:

  letters = list("abcdefghijklmnopqrstuvwxyz")
  scores = [1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10]
The first is just a list of letters and the second is the corresponding scrabble point value for each letter.

Write a program called scrabble.py that asks the user for a word and then figures out the total points for that word. Note: this is a *very* simplified score, as we are neglecting position on the board, frequency of each letter, whether it's an actual word, bingos, and so on...

Also 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.

In your program output, display each letter and it's index in the python list (mostly for debugging purposes), as well as it's point value, like this:

$ python scrabble.py

scrabble word: quartz

letter:  q (index: 16)  score for letter: 10 
letter:  u (index: 20)  score for letter:  1 
letter:  a (index:  0)  score for letter:  1 
letter:  r (index: 17)  score for letter:  1 
letter:  t (index: 19)  score for letter:  1 
letter:  z (index: 25)  score for letter: 10 

total score =  24

$ python scrabble.py

scrabble word: persnickety

letter:  p (index: 15)  score for letter:  3 
letter:  e (index:  4)  score for letter:  1 
letter:  r (index: 17)  score for letter:  1 
letter:  s (index: 18)  score for letter:  1 
letter:  n (index: 13)  score for letter:  1 
letter:  i (index:  8)  score for letter:  1 
letter:  c (index:  2)  score for letter:  3 
letter:  k (index: 10)  score for letter:  5 
letter:  e (index:  4)  score for letter:  1 
letter:  t (index: 19)  score for letter:  1 
letter:  y (index: 24)  score for letter:  4 

total score =  22

3. 5-letter blocks

When encoding and decoding secret messages, the original text is often put into 5-letter blocks, ignoring spaces and punctuation (anything that's not a letter). Write a program that asks the user for some text and then displays the letters from that text in 5-letter blocks, each separated by a space, with no more than 5 blocks per line.

Here are some examples of this program:

$ python 5-letter.py

input text: we love computer science!!

welov ecomp uters cienc e

$ python 5-letter.py

input text: Four Score and seven years ago our fathers brought forth on this continent a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

FourS corea ndsev enyea rsago 
ourfa thers broug htfor thont 
hisco ntine ntane wnati oncon 
ceive dinLi berty andde dicat 
edtot hepro posit ionth atall 
menar ecrea tedeq ual

4. pass the pigs

In the game Pass the Pigs, two pig-shaped dice are used and players roll the dice to get points. Some rolls are worth more than others (e.g., the pigs standing up vs lying on their sides).

Possible rolls and point values for each single pig are as follows:

The scoring for rolling both dice is as follows:

In python, we can use the random library function choice() to simulate a pig-shaped die:

>>> from random import *
>>> pig = ["leftside","rightside","razorback","trotter","snouter","jowler","bacon"]
>>> print choice(pig)
>>> print choice(pig)
>>> print choice(pig)

This assumes all rolls are equally likely, which isn't true, but we'll gloss over that for now...

Write a program called passthepigs.py that simulates rolling two pig-shaped dice and displays the resulting score. To make this a little easier, we are giving you a function called scoreOnePig() that returns the score for a single pig roll. For example, calling scoreOnePig("snouter") returns a 10, scoreOnePig("leftside") returns a 1, and so on (see your passthepigs.py file where we have included the function).

Your program should ask the user how many rolls they want to make and then simulate rolling the "dice" and display the resulting scores:

$ python passthepigs.py

How many rolls? 20

roll: (   trotter,   leftside) -- score:  6
roll: ( rightside,    trotter) -- score:  6
roll: ( rightside,     jowler) -- score: 16
roll: ( razorback,  razorback) -- score: 20
roll: (  leftside,     jowler) -- score: 16
roll: ( razorback,  rightside) -- score:  6
roll: ( rightside,   leftside) -- score:  0
roll: (  leftside,   leftside) -- score:  1
roll: (   trotter,     jowler) -- score: 20
roll: (     bacon,    trotter) -- score:  0
roll: (   snouter,      bacon) -- score:  0
roll: ( razorback,    trotter) -- score: 10
roll: (   trotter,    trotter) -- score: 20
roll: (   snouter,   leftside) -- score: 11
roll: ( razorback,     jowler) -- score: 20
roll: (   snouter,    trotter) -- score: 15
roll: (   snouter,    snouter) -- score: 40
roll: (    jowler,  rightside) -- score: 16
roll: (    jowler,    snouter) -- score: 25
roll: (     bacon,     jowler) -- score:  0

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, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.