Create a file called activity10.py
in cs100/ch2
. Note that it its good practice to keep your function definitions together above and keep your function calls below. For example, your code will always have the following general structure.
import stuff
# function definitions go here
def myFunction1():
<code>
def myFunction2():
<code>
def myFunction3():
<code>
# function calls go here
myFunction1()
myFunction2()
myFunction2()
You will need to use a random number generator for this activity. Here’s how to set it up:
import random
num = random.random() # <-- this puts a random number such that 0.0 <= num < 1.0
# to get a random integer in a certain range do the following:
rInt = int(random.random() * 100)
# The int() truncates the decimal portion, and multiplying by 100 expands the range
# So the variable rInt now contains a random number between 0 and 99 inclusive.
# This program draws the axis for a Cartesian coordinate system
# and then draws 10 random red dots on the canvas.
# It uses a for loop for both actions.
#
# Students will need to modify this program to count how many dots
# are in each quadrant
#
# Author: D. Palmer
#
# Date: Sept. 15, 2022
#
import turtle
import random
# This function draws the coordinate axes using a loop
def drawAxes():
head = 0
t.width(4)
for dir in range(4):
t.pendown()
t.setheading(head)
t.forward(400)
t.penup()
t.goto(0,0)
head = head + 90
# This function draws 10 random red dots on the canvas
# students are tasked with counting how many in each quadrant
def drawRandomDots():
# Initializes all the count variables
northEastCount = 0
northWestCount = 0
southEastCount = 0
southWestCount = 0
for dots in range(10):
x = int((random.random()*800) - 400)
y = int((random.random()*800) - 400)
t.penup()
t.goto(x,y)
t.pendown()
t.pencolor("red")
t.width(10)
t.setheading(0)
t.pendown()
t.forward(1)
# Add a conditional statements here that correctly counts
# the dots in each quadrant
print("There are ", northEastCount, " dots in the Upper Right Quadrant")
print("There are ", northWestCount, " dots in the Upper Left Quadrant")
print("There are ", southEastCount, " dots in the Lower Right Quadrant")
print("There are ", southWestCount, " dots in the Lower Left Quadrant")
t = turtle.Turtle()
#t.speed("fastest")
drawAxes()
drawRandomDots()
# This program draws a 400x400 square in the middle of the canvas
# and then draws 10 random red dots on the canvas.
# It uses a for loop for both actions.
#
# Students will need to modify this program to count how many dots
# are inside and out of the box
#
# Author: D. Palmer
#
# Date: Sept. 15, 2022
#
import turtle
import random
# This function draws a 400 by 400 square in the middle of the canvas
# Students must write this code using a loop
def drawBox():
# to be written by students
# This function draws 10 random red dots on the canvas
# students are tasked with counting how many are inside the box
# and how many are outside the box
def drawRandomDots():
# Initializes all the count variables
insideBox = 0
outsideBox = 0
for dots in range(10):
x = int((random.random()*800) - 400)
y = int((random.random()*800) - 400)
t.penup()
t.goto(x,y)
t.pendown()
t.pencolor("red")
t.width(10)
t.setheading(0)
t.pendown()
t.forward(1)
# Add a conditional statements here that correctly counts
# the dots inside and outside the box
print("There are ", insideBox, " dots in the box")
print("There are ", outsideBox, " dots outside of the box")
t = turtle.Turtle()
#t.speed("fastest")
drawBox()
drawRandomDots()
Change both of these functions to take a parameter to specify the number of random dots to draw.
getMax(a,b,c)
that returns the maximum of three numbers. Test it with:print( getMax(7, -4, 99) )
print( getMax(23, 5, 0) )
print( getMax(17, 74, 9) )
print( getMax(7, 7, -9) )
print( getMax(2, 2, 2) )
print( getMax(7, 9, 9) )
print( getMax(9, 7, 9) )
##Optional - if you finish early
fizzBuzz()
that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”. Call your function to test it. For example, the first few lines printed would appear as follows:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
...
isDivisibleBy(N, x)
which takes a positive integer N
and positive integer x
returns True
if the number x
is divisible by N
and False
otherwise. Test your function by calling it.Submit your working python file to Moodle. It should contain the two random dot counting programs, modified to allow any number of dots - call each with 25 dots. It should also contain the getMax function that works correctly. If you do the FizzBuzz program (a popular interview coding exercise from the 2010’s) and/or the isDivisible() code please include those in your code as well).