CS303E Homework 5

Instructor: Dr. Bill Young
Due Date: Tuesday, September 30, 2025 at 11:59pm

Copyright © William D. Young. All rights reserved.

Assignment

In this assignment, you'll write a program in file LuckySevens.py to play multiple rounds of the dice game of Lucky Sevens. In each game, the user specifies how much money (the stake) they're willing to gamble; this must be a non-negative integer specifying a number of dollars. If the value supplied is not legal, print an error message and try again. Notice that the value supplied is actually a string, so be sure to check that it contains only decimal digits (i.e., that it satisfies .isdigits()) before converting to an int.

After you have determined the stake, repeatedly roll two dice. Each die roll should return a random integer between 1 and 6. (Yes, "die" is the single of plural "dice.") After each roll, display the two dice values and the sum. If the sum is 7, win $4 (increase the stake by 4). Otherwise, lose $1 (decrease the stake by 1). Keep playing until the stake becomes zero. Finally, report how many rolls it took to deplete the stake and what the maximum stake was.

At the end of each game, ask if the user would like to play again. The user's answer can be "yes" or "no". If "no", print a message and end play. Otherwise, play another game. You can assume for this part that the user's answer is either "yes" or "no"; you don't need to validate it.

Notice, it's possible that a game never terminates; the stake could go up indefinitely. But that's very unlikely. Notice that there are 6 possible rolls that add up to 7, out of 36 possible rolls. So the odds of increasing the stake on any particular roll is 1/6; the odds of decreasing the stake on a given roll is 5/6. This isn't a game anyone is going to win in the long run!

This homework is primarily designed to give you some practice using loops, but you'll also need some if statements. Programming this assignment requires at least three loops: one to keep asking until the user inputs a non-negative integer for the stake; one to keep rolling the dice until you run out of money; and a top-level loop to play multiple games. But see the Programming Tips section for a suggestion about programming them incrementally.

Below is some sample output.

> python LuckySevens.py
How many dollars would you like to gamble? $10
Answer must be a positive integer (dollars to gamble).  Try again!
How many dollars would you like to gamble? -10
Answer must be a positive integer (dollars to gamble).  Try again!
How many dollars would you like to gamble? 10.50
Answer must be a positive integer (dollars to gamble).  Try again!
How many dollars would you like to gamble? 10
You've said you're going to gamble $10. Good luck!
1: rolled (6, 5), sum = 11
Sorry, you lost $1. Better luck next time! Your stake is $9
2: rolled (3, 2), sum = 5
Sorry, you lost $1. Better luck next time! Your stake is $8
3: rolled (2, 4), sum = 6
Sorry, you lost $1. Better luck next time! Your stake is $7
4: rolled (4, 4), sum = 8
Sorry, you lost $1. Better luck next time! Your stake is $6
5: rolled (3, 5), sum = 8
Sorry, you lost $1. Better luck next time! Your stake is $5
6: rolled (6, 5), sum = 11
Sorry, you lost $1. Better luck next time! Your stake is $4
7: rolled (4, 4), sum = 8
Sorry, you lost $1. Better luck next time! Your stake is $3
8: rolled (4, 4), sum = 8
Sorry, you lost $1. Better luck next time! Your stake is $2
9: rolled (5, 6), sum = 11
Sorry, you lost $1. Better luck next time! Your stake is $1
10: rolled (4, 5), sum = 9
Sorry, you lost $1. Better luck next time! Your stake is $0

You're out of money after 10 rolls.
Your highest stake was: $10

Would you like to play again (yes or no)? yes
How many dollars would you like to gamble? 5
You've said you're going to gamble $5. Good luck!
1: rolled (3, 3), sum = 6
Sorry, you lost $1. Better luck next time! Your stake is $4
2: rolled (2, 1), sum = 3
Sorry, you lost $1. Better luck next time! Your stake is $3
3: rolled (3, 4), sum = 7
Congratulations, you win $4! Your stake is $7
4: rolled (3, 5), sum = 8
Sorry, you lost $1. Better luck next time! Your stake is $6
5: rolled (4, 2), sum = 6
Sorry, you lost $1. Better luck next time! Your stake is $5
6: rolled (5, 4), sum = 9
Sorry, you lost $1. Better luck next time! Your stake is $4
7: rolled (6, 5), sum = 11
Sorry, you lost $1. Better luck next time! Your stake is $3
8: rolled (2, 3), sum = 5
Sorry, you lost $1. Better luck next time! Your stake is $2
9: rolled (3, 3), sum = 6
Sorry, you lost $1. Better luck next time! Your stake is $1
10: rolled (2, 1), sum = 3
Sorry, you lost $1. Better luck next time! Your stake is $0

You're out of money after 10 rolls.
Your highest stake was: $7

Would you like to play again (yes or no)? no
Thanks for playing!
>
We haven't yet covered lists in the class, so don't use them. But you don't need them. Think about how to find the maximum stake: initially, it's the stake you start with. It only goes down from there unless you roll a 7. If so, increment the stake and ask if it's bigger than the highest stake you've seen so far. If so, that's the new highest stake.

Turning in the Assignment:

The program should be in a file named LuckySevens.py. Submit the file via Canvas before the deadline shown at the top of this page. Submit it to the assignment hw5 under the assignments sections by uploading your Python file.

Your file must compile and run before submission. It must also contain a header with the following format:

# Assignment: HW5
# File: LuckySevens.py
# Student: 
# UT EID:
# Course Name: CS303E
# 
# Date:
# Description of Program: 

As usual, if you submit multiple times to Canvas, it will rename your file name to something like LuckySevens-1.py, LuckySevens-2.py, etc. Don't worry about that; we'll grade the latest version.

Programming tips

Program incrementally: Don't try to write this entire program all at once. In the first pass, you might just handle the input---read numbers until you have a legal input (non-negative integer). Print that and stop. Then add the loop to roll the dice. Print the rolls and sums for 10 rolls, say. (You can do that with a for loop). Then add the updating and printing of the stake. Next replace your for loop with a while, to run until you exhaust the stake. Finally, add the top level loop to play multiple games. Some of you will be thinking: "I don't want to do all those steps; it's easier to just do it all at once." You're wrong!

Using Loops: You usually write a for loop if you know in advance how many times the loop will run and a while loop if you don't. For this problem, you don't know how many times the loop will run (iterations). So you have to figure out the loop test. The trick is to make sure that the loop test makes sense the first time you enter the loop.

Sometimes, it makes sense to write a loop test of True when you don't know how many iterations there will be. But then you run the risk of an infinite loop. You have to test inside the loop body to see if you are ready to exit and break if so. If not, you can always continue to another iteration of the loop. Years ago, I worked on a programming language called "Gypsy" in which all loops were like this. The loop statement had no test and the only way to exit a loop was an explicit break statement. That may be why I often write loops that way.