13.5. Week 5 Lab

Material Covered

Basic functions, listener functions and reading in files (Chapter 5)

13.5.1. Level 1

The following function is missing a docstring with pre and post conditions. The objective of this level is to add the missing docstring.

  1. Add the missing docstring. You are to find out what is needed for the function to run (aka pre-conditions) and what the output will be/how the program will be altered by running the function (aka post-conditions). You do not need to add or delete any code in this level, only add the docstring.

Tip

There are 3 pre-conditions and 2 post conditions

13.5.2. Level 2

In this level you will create a turtle function that will draw a random line in the window when a key is pressed.

  1. The starter code below imports modules and creates a window and a turtle.

  2. Create a function called goto_random(). Inside this function, create two variables to store random x and y coordinates. Assign these variables random values that range from -200 to 200 (these are left/bottom and right/top window edges). Use the randrange() function to get random numbers.

  3. Add a goto() statement that will send the turtle kyra to a random spot in the window, using the variables from the previous step.

  4. Create another function called reset(). This function should return the turtle to the center of the window and clear all the pen lines that the turtle has drawn.

  5. At the bottom of the program, call the reset function. This ensures the turtle window pops up, since a turtle command is called.

  6. Under the reset function call, register the two functions so that they respond to the correct user key presses. The reset() function should be called when the r key is pressed and the goto_random() function should be called when the f key is pressed.

  7. Finally, add the wn.listen() command at the end. This must be the last line.

  8. Test to make sure this works. Click in the window, then hit the ‘f’ key a few times to make sure the turtle moves around. Then hit the ‘r’ key to ensure the turtle returns to the center and all the lines disappear.

Tip

Reference the textbook, Section 5.5 on listener functions if you have difficulty with the syntax for setting up the key press listeners.

Below is a clip of how your program should work.

13.5.3. Level 3

This level is similar to Level 2, except instead of getting random coordinates in response to key presses, you will read a file containing turtle coordinates and then send a turtle around the window using those specific coordinates.

Tip

Take a look at how the file is formatted before you start to code. The coordinate file is shown at the bottom of this page.

  1. The provided code sets up a world and a turtle, and opens the file with the coordinates for reading. Run the code to make sure it works. Nothing happens yet.

  2. Iterate through each line of the file. See Section 5.9 in the textbook for how to write a for loop that does this, in particular, look at activity 5.9.3 as an example. For each line, do the following:

    1. Split up the coordinates (assign them to a variable called coords)

    2. Access the two elements of coords (using coords[0] and coords[1]), using them in a goto() function to send the turtle to those coordinates

    3. Print out the following message to the console: “Going to: <xcoordinate> <ycoordinate>”.

  3. Once you have written this code, run it to make sure that the turtle does a walk through the coordinates and the coordinates print out to the console.

Your output should look like this:

../_images/turtle_coords_output.png
Data file: turtle_coords.txt
10 125
-45 67
67 42
-20 -180
173 -2
16 194
-87 91
0 -63
You have attempted of activities on this page