CPSC120B
Fundamentals of Computer Science

Lab 8

Modules

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.

Example

Enter the number of rolls: 3
2 2
1 2
4 1
The average roll is 4.0

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 drawing in general.

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

Hint

  • You are going to approximate the sine wave curve by simply sampling the sine function. 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. You then simply have to goto each one of these points in a sequence.
  • 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 by amplitude.
  • 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.


Back before modern flat LCD displays, computers used Cathode Ray Tube, CRT, displays. Besides being big and heavy, the displays suffered from burn-in. If the same image was left on the display too long, a ghost of it would become permanent. So, if a computer was not used for a short duration, it would play an animation to save the screen from burn-in. We don't need screen savers anymore, but computer still come with them because they are fun to look at.

Details

Create a python program that uses the graphics module to draw lots of ovals of random sizes in random locations. The ovals can be any size, but random locations must be limited to and fill the dimensions of the window.

Example

Hint

  1. Use a for loop and the graphics module's draw_oval function to draw many ovals.
  2. Use the random module's randrange function to generate a random x-coordinate, y-coordinate, width, and height for each oval.
  3. Use the graphics modules window_width and window_height functions to limit the range of the x and y coordinates.

Challenge

After awhile the screen is all black, not a very interesting animation. In order for the amimation to continue longer, it should have lots of colors. Make each oval drawn a random color.