CS21 Lab 4: while loops, random numbers

Due before 11:59pm Saturday, February 22nd, 2014

This lab assignment requires you to write two 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. Under Over

For this program, underOver.py, you will write a simulation of a dice game called Under Over. The game is played by rolling two six-sided dice and betting on one of three possible outcomes:

  1. The sum of the two dice is less than seven.
  2. The sum of the two dice is seven.
  3. The sum of the two dice is greater than seven.

The player starts with 100 chips. For each round of the game, the player automatically bets 10 chips, so you do not have to ask the player how many chips to bet. You will prompt the player to select one of the three possible outcomes (under, over, or seven), or 'quit' to end the game. The only valid choices are: under, over, seven, or quit. If the user enters in anything other than those four strings exactly as shown, you will let the player know the input was not valid and prompt again for another choice. You will keep prompting for a choice until a valid choice is entered.

Once the player has selected a choice, you will play the game by simulating the roll of two six-sided dice and get the total of the two dice. You will compare the total to the players selection to determine if the player has won or lost. You will print the total of the dice roll to the player, and let the player know if he or she won or lost. If the player lost, you will subtract the amount of the bet (10 chips) from the players total. If the player won, you will let the player know how many chips he or she has won and then add it to the player's chip total. For a correct bet of 'over' or 'under', the player wins the amount of the bet (10 chips). For a correct bet of 'seven' the player wins four times the player's bet (40 chips).

If the player loses all of his or her money, you will let the player know and automatically end the game by quitting the program. If the player quits, you will let the player know how many chips he or she has left.

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

from random import *

You should use randrange or randint functions 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 two sample runs of such a program:

 
$ python underOver.py

Welcome to the Under Over game.  The computer will roll two dice and
allow you to bet on whether the total of the two dice are under seven,
over seven, or exactly seven.

You have 100 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': Fred
Fred is not a valid choice.  Try again.

Valid bets are 'under', 'over', or 'seven': 7
7 is not a valid choice.  Try again.

Valid bets are 'under', 'over', or 'seven': Quit
Quit is not a valid choice.  Try again.

Valid bets are 'under', 'over', or 'seven': under
The computer rolled a 3.
You won 10 chips!

You have 110 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': over
The computer rolled a 7.
Sorry, you lost 10 chips.

You have 100 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': seven
The computer rolled a 8.
Sorry, you lost 10 chips.

You have 90 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': over
The computer rolled a 5.
Sorry, you lost 10 chips.

You have 80 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': seven
The computer rolled a 10.
Sorry, you lost 10 chips.

You have 70 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': under
The computer rolled a 5.
You won 10 chips!

You have 80 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': quit
You leave with 80 chips.
Thank you for playing.

$ python underOver.py

Welcome to the Under Over game.  The computer will roll two dice and
allow you to bet on whether the total of the two dice are under seven,
over seven, or exactly seven.

You have 100 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': seven
The computer rolled a 8.
Sorry, you lost 10 chips.

You have 90 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': under
The computer rolled a 7.
Sorry, you lost 10 chips.

You have 80 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': seven
The computer rolled a 12.
Sorry, you lost 10 chips.

You have 70 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': seven
The computer rolled a 5.
Sorry, you lost 10 chips.

You have 60 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': seven
The computer rolled a 11.
Sorry, you lost 10 chips.

You have 50 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': seven
The computer rolled a 11.
Sorry, you lost 10 chips.

You have 40 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': seven
The computer rolled a 10.
Sorry, you lost 10 chips.

You have 30 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': seven
The computer rolled a 10.
Sorry, you lost 10 chips.

You have 20 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': seven
The computer rolled a 5.
Sorry, you lost 10 chips.

You have 10 in chips.  Make a bet or type 'quit' to exit.
Valid bets are 'under', 'over', or 'seven': seven
The computer rolled a 4.
Sorry, you lost 10 chips.

You are out of money.
Thank you for playing.

2. Python Calculator

Write a program called calculator.py that will perform addition, subtraction, multiplication, and division. It will accept floating-point values and it will check for division by zero.

Input will be in the form:

            <operand> <operator> <operand>

where <operand> can be any integer or floating point number, and <operator> is one of: +, -, *, /

To simplify the assignment, the operator must be separated from the operands by at least one space, for example, "4 + 3". If this is not the case, for example, "4+3", you can treat this as an illegal operand.

When you hit the Enter key, the result will be printed. If an illegal operand is used or division by zero is attempted, an error message will be printed, but the program will continue asking for a new expression to evaluate.

The user may also enter quit or q from the "Input:" prompt to quit the calculator. In this case, the program will exit and print "Goodbye."

A sample run of the program is shown below:

$ calculator.py

Welcome to the Python Calculator.

Enter an expression in the form of: <number> <operator> <number> where
<number> is any integer or floating point number and <operator> is one
of: +, -, *, or /. Numbers and operators must be separated by spaces.
Type 'quit' or 'q' to quit.

Input: 5 * 6
30.0 

Input: 4 + 5
9.0 

Input: 3.5 - 5
-1.5 

Input: 3.9 / 1.432
2.72346368715 

Input: 5 & 6
Invalid input.

Input: 5*6
Invalid input.

Input: help
Invalid input.

Input: 1 + 2 + 3
Invalid input.

Input: 5 / 0
Invalid input.

Input: 4 * 3 5 
Invalid input.

Input: exit
Invalid input.

Input: quit
Goodbye!

Notes:

Credits:

This part of the lab was based off the Calculator assignment from the CS1 Python Programming Projects Archive and is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.


3. Hacker's Challenge: flexible calculator input, odds of winning Under Over

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

For the first hacker's challenge, make a version of the calculator program that will accept an input with or without spaces, e.g., 5+3. Call this program calculatorPlus.py.

For the second hacker's challenge, determine if the odds for Under Over are fair. There a few ways to do this. One way is to mathematically compute the odds of getting an under, over, and seven and see the payouts are favorable. Another way is to write a version of the program that plays the game thousands and thousands of times (without betting), keeping track of the outcomes of each game. If you run your simulations enough times, it should be pretty close to the mathmatically computed odds for each of the outcomes. Call this program underOverOdds.py.


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