As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.
$ cd ~/cs120/labs $ mkdir lab8 $ cd lab8
Write a Python program that will print the result of rolling 12 six sided dice to the terminal window.
$ python3 practice1.py 2 1 4 3 2 6 5 4 1 2 1 1
Write a Python program that can compute the number of pairs that can
be created from our CPSC 120 class. You should use
the input
function to determine how many students are
in your class.
The number of combinations of size k that can be created from a set of size n can be computed using the binomial equation:
\[ \binom nk = \frac{n!}{k! \cdot (n - k)!} \]$ python3 practice2.py How many students are in your class? 20 There are 190 pairs.
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.
In Emacs, create a Python program in a file called sine.py
that uses the turtle module to draw a function plot of
the math.sin
function. The program should prompt the
user for the amplitude of the sin function, in turtle window units.
Test your program by running it multiple times with different input values. Make sure your code follows the courses code conventions.
$ python3 sine.py Enter the amplitude: 100
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.
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
.
math.sin
by the amplitude as well.
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.
In old CRT monitors if the same image was displayed for too long, a ghost of the image would get 'burned' into the screen. To prevent this from occuring a screensaver would run whenever the computer was inactive for a period. Modern LCD monitors no longer need screen savers, but they can still be fun to watch.
In Emacs, create a Python program in a file called screen_saver.py that uses the turtle module to draw many dots on the screen. The dots should be in random locations, centers located anywhere in the window, have random sizes, in the range [100, 300], and should be random colors.
Use
the turtle.dot
function to draw the dots.
Use
the turtle.pencolor
function to change the color of the dots.
Use
the window_width
and window_height
functions to determine the width and height of the window.
Make sure your code follows the courses code conventions.
$ python3 screen_saver.py
turtle.dot
function.
random.randrange
function to generate
random numbers in the range [100, 300]. Remember
the stop
argument is exclusive.
turtle.pencolor
function takes three numbers,
one each for red, blue, and green, to specify a color. To
generate a random color, you will need to generate three
random numbers.
The random dots screen saver is good, but is pretty silly.
Instead, you already have a program written that can draw polygons
with an arbitrary number of sides. Write a new program
called screen_saver_poly.py
, which generates a large
number of polygons with a random number of sides in the range
\([3, 20]\). These shapes should also be filled in a random color.