Exercises for Lesson 15
Exercise 1: Review: while loops, \n, and break keyword
Write a program that asks the user to enter letters, one at a time, concatenating them into a single string until they enter a vowel.
Here’s an example interaction with the program:
Please enter characters, one at a time (a vowel to stop), pressing enter after each:
r
s
t
l
n
e
You entered: rstl (+e)Exercise 2: Rolling random dice
Part a: Reading a program
Read through the following program, trying to understand what it’s doing. (After class, type it up and run it – as you increase n, the results should get ever closer to an even 25% split.)
import random
def getInputs():
return int(input("How many dice? "))
def rollD4():
return random.randint(1,4)
def rollDice(n):
counts = [0]*4
for i in range(n):
val = rollD4()
counts[val-1] += 1
return counts
def printResults(rolls):
for i in range(len(rolls)):
rollCount = rolls[i]
print(i+1, rollCount, rollCount / sum(rolls))
def main():
n = getInputs()
diceRolls = rollDice(n)
printResults(diceRolls)
if __name__ == "__main__":
main()Part b: Documenting a program
Write a docstring for each function (except main) in this program.
Part c: Diagramming a program
Make a diagram showing how this program adheres to top-down design.
Exercise 3: Dice games
In the game of Yahtzee, each player rolls five dice (with some re-rolls), and scores dice based on the poker-like “hand” they get. One such result is “four of a kind”, which is satisfied when at least four of the five dice have the same value. In this exercise, you’ll work your way backward relative to the previous exercise, building up such a program.
The goal of the program is to roll random hands of five six-sided dice, counting how many such hands (out of, say 1000), satisfy the “four of a kind” requirement.
Part a: Diagramming a program
Make a diagram to describe this program using top-down design.
Part b: Documenting a program
Write shell functions (e.g., either pass or return None or something similar) to fill in main. Write the docstring for each function you’ll need.
Part c: Write test cases
Before actually writing code, try to write some test cases for determining whether something satisfies the “four of a kind” requirement.
Part d: Write your program
Finally, fill in each function, testing as you go!
Exercise 4: More dice games
Work through the same exercise from before, but for different types of hands. What do you have to change in your designs?
Part a: Yahtzee!
Modify your design and program to check for a Yahtzee, which is when all five dice are equal.
Part b: Another hand type
Modify your design and program to check for a “full house”, which is when three dice have one value, and the other two have another. For example, dice values 3,3,3,4,4 would be a full house.
Part c: More dice
Modify your design and program to change the rules of the game and have a different number of dice, for example 4 or 6. How would your code from Part (a) need to change to handle a different number of dice?