Skip to main content

Exercises 24.9 Chapter Exercises

1.

The code below is supposed to roll an 8-sided die (sides numbered 1-8) 10000 times and after it is done, print out how many times 1 was rolled.
Move the correct blocks from the left to the right and indent them properly. You will not need to use all of the blocks. Make sure to put import random first.

2.

The code below is supposed to “flip a coin” 100 times by making a random number that is 0 (heads) or 1 (tails) and keep track of the longest streak of heads that we see. For each repetition, we need to make a random number, then check to see if it is a “heads” (0). Then we will updatethe headsInARow counter (like the sample from Building a Simulation) based on that result by adding one or resetting it to 0. If the headsInARow increased (but not if it was reset to 0), we will check and see if it is bigger than longestStreak. If so, we will update the longestStreak to match headsInARow. After all 100 flips are done we will print out the longestStreak.
Move the correct blocks from the left to the right and indent them properly. You will use all of the blocks. Make sure to put import random first.

3.

Using the code below, write a program that simulates rolling a pair of six-sided dice (1-6) 10000 times and counting up the number of times that an eight is rolled (the two dice add to 8).
Make sure to generate two separate numbers between 1-6 for each roll. Then add those two numbers up and if they make eight, increment the numEights counter.
If you have trouble, try reducing the range to something like 10 or 20 rolls. For each roll, print out what you rolled for each of the dice and the total they make.
Warning There is no autograder for this problem. It is up to you to decide if your program is working. For 10000 trials you should usually get an answer between 1300 and 1500. You may occasionally get a result outside that range if you are really unlucky or really lucky.

4.

A friend of yours likes a game that involves rolling dice to create their characters.
Finish the program below to simulate rolling three dice (with sides 1-6) 6 different times and keeping track of the highest total that you saw.
For example: Roll 1, 3, 5 - total is 9. The highest roll so far is now 9 Roll 4, 6, 3 - total is 13. The highest roll so far is now 3 Roll 2, 1, 5 - total is 8. The highest roll so far is still 13 Roll 6, 5, 5 - total is 16. The highest roll so far is now 16 Roll 3, 2, 5 - total is 10. The highest roll so far is still 16 Roll 4, 4, 3 - total is 11. The highest roll so far is still 16
Warning There is no autograder for this problem. It is up to you to decide if your program is working.

5.

This program currently makes a pink dot that is 50 pixels across and is centered at 0, 0.
Modify the program so that the size of the dot is a random value between 50 and 200 and the location it draws it is a random location where both x and y are random numbers between -100 and 100.
The automatic tests will make sure you generate appropriate values, but it won’t necessarily verify you used the random numbers you make in the correct spots. You are responsible for checking that yourself!

6.

Challenge.
Finish the function getOddsFor. It should determine the odds of rolling targetNumber when rolling two six-sided dice (1-6). It should do so by using a loop to repeat TRIALS number of times rolling two dice and seeing how many times targetNumber is the total of the two dice. Then return that number divided by TRIALS.
This is very similar to exercise 3 above… Just make sure to check if the total you “roll” is the same as the targetNumber as opposed to always checking against 8.
Make sure to return your answer from the function, don’t print it in the function!
You have attempted of activities on this page.