Exercises for Lesson 8
Exercise 1: Drawing a shape
Adapt our simple code snippet to draw a triangle into a function that takes in a list of Point
objects, a color name, and a GraphWin
object and draws a triangle in the window. The function signature and main
are given for you. Also, the docstring helps users of your function learn about what it does and how to use it.
from graphics import *
def drawTriangle(pointList, colorName, win):
"""
Draws a triangle in the given window.
pointList: the Point objects that give triangle bounds (a list)
colorName: the color (a string)
win: the window in which to draw (a GraphWin)
"""
# TODO
def main():
win = GraphWin("Triangle!")
p1 = win.getMouse()
p1.draw(win)
p2 = win.getMouse()
p2.draw(win)
p3 = win.getMouse()
p3.draw(win)
points = [p1, p2, p3]
drawTriangle(points, "thistle", win)
win.getMouse()
main()
Exercise 2: Drawing many shapes
Write a function that takes in lists of colors and radii and a Point
, and draws concentric circles centered at that Point
. The function signature and main
are given for you.
from graphics import *
def drawCircles(colorList, radiusList, point, win):
"""
Draws circles all centered at the same point.
colorList: color of each circle (a list of strings)
radiusList: radius of each circle (a list of integers)
point: the center of each circle (a Point)
win: the window in which to draw (a GraphWin)
"""
# TODO
def main():
win = GraphWin("Circles!")
colors = ["purple", "blue", "green", "yellow", "orange", "red"]
radii = [100, 80, 60, 40, 50, 10]
center = Point(100, 100)
drawCircles(colors, radii, center, win)
win.getMouse()
main()
Exercise 3: Creating and drawing shapes
Modify your function from above to not only draw the triangle, but also return it. The updated signature, docstring, and main
are given to you.
from graphics import *
def drawTriangle(pointList, colorName, win):
"""
Draws a triangle in the given window.
pointList: the Point objects that give triangle bounds (a list)
colorName: the color (a string)
win: the window in which to draw (a GraphWin)
returns: the created triangle object (a Polygon)
"""
# TODO: copy your previous code, but now also return
def main():
win = GraphWin("Triangle!", 300, 300)
p1 = win.getMouse()
p1.draw(win)
p2 = win.getMouse()
p2.draw(win)
p3 = win.getMouse()
p3.draw(win)
points = [p1, p2, p3]
triangle = drawTriangle(points, "blue", win)
# Make a copy of the triangle (not an alias!)
triangleCopy = triangle # bug! use .clone() instead
triangleCopy.setFill("yellow")
triangleCopy.move(100, 0) # move 100 to the right
triangleCopy.draw(win)
win.getMouse()
main()
Exercise 4: Writing functions that return values
Part a: Summing values
Write two functions:
sumN(n)
returns the sum of the firstn
natural numbers, i.e., 1+2+3+…+nsumNSquares(n)
returns the sum of the squares of the firstn
natural numbers
Here are their signatures:
def sumN(n):
"""
Computes the sum of the first n natural numbers.
n: the number of ints to sum (an int)
returns: the sum of numbers 1-n (an int)
"""
# TODO
def sumNSquares(n):
"""
Computes the sum of the squares of the first n natural numbers.
n: the number of ints' squares to sum (an int)
returns: the sum of the squares of 1-n (an int)
"""
# TODO
Now, write a program that asks the user for a value of n
, and uses these functions to print out the sum of the first n
natural numbers and the sum of their squares.
def main():
n = # TODO
# TODO
Question: does the code still run even if you name your variable something else in main
?