CS21 Lab 4: while loops, random

Due before Midnight Saturday 5 October 2013

This lab assignment requires you to write three programs in python. First, run update21. This will create the cs21/labs/04 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/04 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/04
$ pwd
/home/your_user_name/cs21/labs/04
We will only grade files submitted by handin21 in this labs directory, so make sure your programs are in this directory!

1. Credit card payoff

Write a program called credit.py that prompts the user to enter a starting balance for a credit card, an annual interest rate, and a monthly payment. You program should then display a table of the monthly balance of the credit card until the balance is $0.00. Your program should then print the total number of months needed to pay the balance and the total amount paid (principal and interest).
Since the annual interest rate is given as a percentage, the monthly rate is month_rate=annual_pct/(100.*12). When updating the credit card balance, subtract the monthly payment first. Then compute and add the monthly interest based on remaining balance only if the remaining balance is greater than zero.

interest = current_balance*month_rate
If the total balance at the start of the month is less than the monthly payment, only pay the remaining balance and set the balance to zero.

Here are two sample runs of such a program:

$ credit.py

Enter a balance: 1000
Enter annual interest rate: 18
Enter monthly payment: 100

month  balance
--------------
  1 $   913.50
  2 $   825.70
  3 $   736.59
  4 $   646.14
  5 $   554.33
  6 $   461.14
  7 $   366.56
  8 $   270.56
  9 $   173.12
 10 $    74.21
 11 $     0.00

Total paid over 11 months: $1074.21

$ credit.py

Enter a balance: 2500
Enter annual interest rate: 20
Enter monthly payment: 300

month  balance
--------------
  1 $  2236.67
  2 $  1968.94
  3 $  1696.76
  4 $  1420.04
  5 $  1138.71
  6 $   852.69
  7 $   561.90
  8 $   266.26
  9 $     0.00


Total paid over 9 months: $2666.26

2. Baseball batter simulation
In this program, you will write a simplified simulation of a baseball/softball batter. A baseball pitch thrown to a batter can result in a ball, strike, or foul (in this simplification, the batter never hits a fair ball).

If a batter accumulates four balls, the result is a walk. If the batter accumulates three strikes, the result is a strike out. A foul counts as a strike, unless the batter already has two strikes.

You should write program baseball.py to simulate a batter by repeatedly choosing one of the three types of pitches (ball, strike, foul) until the batter walks or strikes out. For each pitch, you should display the type of pitch and the total number of balls and strikes.

When implementing your solution, you should import the random library at the top of your program using.

from random import *
You may use the choice function to pick a random pitch: choice(["ball", "strike", "foul"]).

Here are some sample runs of such a program:

 
$ python baseball.py
pitch  1: strike  count 0-1
pitch  2: strike  count 0-2
pitch  3: ball    count 1-2
pitch  4: foul    count 1-2
pitch  5: foul    count 1-2
pitch  6: strike  count 1-3
Struck out after 6 pitches

$ python baseball.py
pitch  1: ball    count 1-0
pitch  2: ball    count 2-0
pitch  3: foul    count 2-1
pitch  4: foul    count 2-2
pitch  5: foul    count 2-2
pitch  6: ball    count 3-2
pitch  7: ball    count 4-2
Walked after 7 pitches

$ python baseball.py
pitch  1: foul    count 0-1
pitch  2: strike  count 0-2
pitch  3: foul    count 0-2
pitch  4: strike  count 0-3
Struck out after 4 pitches

3. Dice game

Write a program called dicegame.py which performs a number of simulations of the following dice game.

A player rolls two six sided die. If the sum of the two dice is not seven, the sum is added to the player's total score and the player gets to roll again. When the player rolls a sum of seven, the game is over.

You program should prompt the user for a number of games to simulate and then play that many games. After simulating all games, you program should display the highest score in a single game, the lowest score, and the average score over all games.

You must read the number of games to simulate in using raw_input and verify that it is a valid positive integer. If the user types in invalid input, display a brief error message and keep asking for a valid input until the user types an appropriate response.

You should use randrange to generate a valid die roll with a value between one and six, inclusive. Note that every time you call randrange() a new value is generated. If you need to save the result of a die roll, save it in a variable.

Here are some sample runs of such a program:

 
$ python dicegame.py
How many games do you want to simulate?2
After 2 simulations:
Best: 122
Worst: 33

Average: 77.5

$ python dicegame.py
How many games do you want to simulate?10

After 10 simulations: 
Best: 168
Worst: 0

Average: 41.9


$ python dicegame.py
How many games do you want to simulate?100000

After 100000 simulations: 
Best: 439
Worst: 0
Average: 35.1

You should design and test your solution to this problem incrementally. Make sure that your solution correctly simulates one game by printing out each roll and ensuring that the total, best, and worst scores are computed correctly. Once you are confident your solution works, you may remove the printing of each roll and run the full simulation.

3b. Hacker's Challenge: Interactive

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 an interactive version of the dice game with the following twist: after each roll, the user should be asked if he/she wants to roll again. If the user decides to quit, she/he keeps her/his current total. If the user rolls a seven though, the player loses all accumulated points.

After each game, ask if the user would like to play again. Once the user answers 'no', display a summary displaying the best, worst and average score. Can you beat the average of the simulation in problem 3?

Roll: 6, total 6
roll again? y/n:y
Roll: 5, total 11
roll again? y/n:y
Roll: 4, total 15
roll again? y/n:y
Roll: 8, total 23
roll again? y/n:y
Oh no, you lost after 23 points

play again? y/n:y
Roll: 8, total 8
roll again? y/n:y
Oh no, you lost after 8 points

play again? y/n:y
Roll: 11, total 11
roll again? y/n:n

play again? y/n:y
Oh no, you lost after 0 points

play again? y/n:y
Roll: 9, total 9
roll again? y/n:y
Roll: 4, total 13
roll again? y/n:y
Oh no, you lost after 13 points

play again? y/n:y
Roll: 3, total 3
roll again? y/n:y
Roll: 5, total 8
roll again? y/n:y
Roll: 9, total 17
roll again? y/n:y
Roll: 8, total 25
roll again? y/n:n

play again? y/n:n
Games played: 6
Best: 25
Worst: 0
Average: 6.0
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/04 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/04 directory. For example:

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