For this assignment you will use Blackboard to submit a single .py file containing solutions to the following programming exercises. You may use the file hw2.py as a template. Carefully test each function to make sure that it works as expected. These exercises will be graded both on functionality and code quality.
polygonXY
that takes an
x and y position to begin drawing. The new version of the
function should have the
signature polygonXY(x, y,
sides, size)
. Use penup
and pendown
to ensure that there are no lines drawn
as the turtle is moving to the new starting position. (5pts)
def drawPolygons( polygons ): """Use turtle graphics to draw a collection of polygons. Arguments: polygons - A list containing polygon locations and specifications. Each entry in the list should be a list with four entries: xpos, ypos, sides, size. For example: drawPolygonList([ [0, 0, 3, 50], [50, 100, 4, 20] ]) would draw a triangle at position (0, 0), and a square at position (50, 100). """ #Your code goes here... #iterate through the list of polygons using a for loop. #For each entry, call polygonXY.
def readPolygonFile( name ): """ Returns the list of polygons stored in the provided file. Arguments: name - A string containing a file name. Each line of the indicated file must contain four integers specifying the position and shape of a polygon. The format for one polygon is: xpos ypos sides shape For example, the following two lines: 0 0 3 50 50 100 4 20 would encode a triangle at position (0, 0), and a square at position (50, 100). The appropriate return value would be: [ [0, 0, 3, 50], [50, 100, 4, 20] ] Returns: a list of polygon specifiers, each of which is a list containing four integers. """ #your code goes here.
drawPolygonFile( name )
that
takes a file name as a parameter, and draws the polygons
stored in that file. If you have finished the previous
two exercises, this should only require two lines of
code. You can test your function on the following two files:
simple.txt
and picture.txt. Since the
second file contains over four hundred polygons, it will
take a long time to draw. You can speed up the process
using the turtle module's speed
function. (2pts)
lastWords(string)
. This function should
take a string argument containing multiple lines of text as input, and
should return a new string containing the last word from each line
of the original string. For example, after the following code segment
text = """My main goal is to convince everyone to recycle at least one aluminum can every day. Following this program protects wildlife, including the rare Python. """ message = lastWords(message)the variable
message
should contain the string "everyone
can program Python."
splitlines
method that
returns a list of lines in the string.