Dice
With the random module you can create programs that appear to be unpredicable, like the rolling of dice.
Details
Create a python program that simulates rolling a pair of six-sided dice. The program should prompt the user to enter the number of rolls, print all of the rolls, and the average of the rolls.
-
Inside of a loop, use the random module's
randrange
function to repeately generate two integers in between 1 and 6. - Use an accumulator to compute the sum of all of the dice rolls.
- After the loop, use the accumulated sum to compute the average.
Example
Enter the number of rolls: 3 2 2 1 2 4 1 The average roll is 4.0
Challenge
What's the difference between using two six-sided dice and three four-sided dice? They both have a maximum value of 12, but do they have different average values? Write program that simulates rolling both kinds of dice and prints the average of both. Be sure to "roll" the dice many times to get an accurate average.
Sine
Hopefully you are familiar with the sine and cosine functions from back in your trigonometry class. As you can imagine, these are incredibly useful functions not only for mathematics, but for computer drawing and animation.
Details
Create a Python program that uses the turtle module to draw
a plot of the math.sin
function. The program should
prompt the user for the amplitude and period of the sin function, in
turtle window units.
Example
Enter the amplitude: 100 Enter the period: 600

- You are going to approximate the sine wave curve by sampling the sine function. Sampling is the process of computing something for some points in a space, as opposed to computing something for the whole space. You are going to choose a set of equally spaced points on the curve. Use a for loop that starts at the left part of the window, and increases the value by a fixed step until it gets to the right portion of the window.
-
Treat the input to the
math.sin
function as the x coordinate of the point on the turtle window, and the return value as the y coordinate. The turtle should go to each one of these points in a sequence using theturtle.setposition
function. -
The
math.sin
function always returns a value between -1 and 1. You need to use the amplitude to increase the size of this output, so you can visually see the actual sine wave. You can do this by multiplying the output byamplitude
. -
The
math.sin
function's input is an angle in radians. If the input x coordinte is not scaled, the period of the plot will be 2 π. To change the period, multiply the input x coordiate by 2 π over the desired period.
Challenge
The sine and cosine functions are interesting, but I think the tangent function is a much more visually appealing function. Change the color of the turtle's pen, and also plot the tangent function on top of the sine function.
Polygon Radius
Create a Python program that prompts the user for the perimeter and number of sides of a regular polygon and prints the distance from the center of the polygon to a vertex of the polygon. For example:
The line d in the above octagon is the distance from the center to a vertex.
Example
Enter the polygon's perimeter: 100 Enter the number of sides of the polygon: 8 The distance from the center to a vertex is 16.332037060954708
- Note that d is the hypotenuse of a triangle. So to find d, use some trigonometry. In particular, sin(theta)=opposite/hypotenuse.
- Solve for the hypotenuse. To create an equation for d.
- Compute theta from the number of sides the polygon has.
- Compute the length of the opposite side from the polygon's perimeter and number of sides.
Challenge
When drawing polygons, it is often easier to specify the radius and the not the perimeter. Create a program that does the opposite of the program you already wrote. The program should prompt the user for the radius of a polygon and number of sides. Then it should print the length of each side.