12.9. Chapter Exercises

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.

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. It will do so by first updating the headsInARow counter (like the sample from the Building a Simulation page). 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.

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 seperate 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 troubles, try reducing the range to something like 10 or 20 rolls and printing out what you roll for each of the dice and the total them make. When you get it working, make sure to reset the loop to 10000 trials or you won’t pass the tests.

Note: It is possible, but extremely unlikely that you will get really lucky or unlucky and the tests will think your code is wrong even if it is right. If that happens, just run it again.

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.

Your code from the previous problem should be a guide for most of the process. 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 can change TRIALS while debugging so that you can print information and mange to read it all. But make sure to change it back to 100000 (or a similar large number) for the final run.

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!

Turtle programming reference sheet

You have attempted of activities on this page