![]() |
Cereal Box Celebrities |
10 points
Assume that a certain company's cereal boxes ship with a laminated celebrity photo. There are 5 different possible photos,
with some more likely than others. Below are the celebrities and the percent of cereal boxes that contain that celebrity's photo.
There is no indication on the outside of the box as to which photo is inside.
- 30% have Taylor Swift
- 20% have Harry Styles
- 20% have Drake
- 15% have Lady Gaga
- 15% have Ed Sheeran
The program should answer the question: About how many cereal boxes would you have to purchase in order to get at least 1 photo of each of the 5 celebrities? We will answer the question by simulating buying cereal boxes, in an effort to collect all 5 photos. We use random numbers in the simulation, since there is randomness in which photo is in a box of cereal that you buy.
You could set up a WHILE loop that runs until at least 1 photo of each celebrity has been obtained. You could use a list to store the names of the celebrities whose photos you've obtained. You could use random numbers to simulate buying cereal boxes. Take Taylor Swift, whose photo is in 30% of the cereal boxes. You could generate a random integer 'num' between 1 and 100. You can then decide that if 'num' is between 1 and 30, the box contains Taylor Swift. You could take a similar approach for the other 4 celebrities. Once you have a photo of all 5 celebrities, print out how many cereal boxes you had to purchase.
Some useful Python:
To create a list: myList = []
To add an element to a list: myList.append("Swift")
To find out how many elements are stored in a list: len(myList)
To check if "Swift" is in 'myList': if "Swift" in myList:
To check if "Swift" is NOT in 'myList': if "Swift" not in myList:
To create random numbers:
- put 'import random' at the top of the program
- use 'random.randint'. For example, number = random.randint(1, 100) generates a random integer between 1 and 100 (inclusive)
To write an IF statement that runs if 2 conditions are both true: if something1 and something2:
Structure of an IF statement with 3 or more possibilities:
if something1:
...
elif something2:
...
else:
...
You don't create a GUI interface for this program. You just print out your result. Here's a screenshot:
Download CerealCelebrities.py as a starting point for the project.
3 points
In main(), run 10,000 simulations of this process, and calculate the average number of boxes you'd need to buy in order to obtain all 5 photos.