CS 110 Spring 2012
Homework 03
Due: Thursday, 9 February in class
In Homework 3 you will practice function definition building with TurtleWorld graphics along with some additional short programs.
Part 1 - Building TurtleWorld Functions
The following series of exercises are based on Chapter 4 from your textbook. If you run into difficulty designing and building these functions, you should read that chapter thoroughly for help. Please think intentionally about the point of each of these exercises, as they are not busy work, but are helping us to think through the process of developing solutions to problems.
Step 1: Framework for the program and the main function
Begin by opening a new python program file named polygon.py and starting with the code given below.
# Required header comments # from TurtleWorld import * def main(): world = TurtleWorld() bob = Turtle() print bob # In this section of the main function, you will add invocations to carefully # test your function definitions. # ... wait_for_user() #------------------------------------------------------------------------------- # In the following portion of the python source file, you will create all of # your new function definitions, all of which should appear _before_ the # conditional invocation of main() below. # ... # End of your function defintions #------------------------------------------------------------------------------- if __name__ == '__main__': main() #-------------------------------------------------------------------------------
Step 2 - Add Functions
- Write a fuction called square that takes a parameter named t, which is a turtle. It should use the turtle to draw a square.
- Add another parameter, named length, to your definition of function square. Modify the body so the length of the sides of the square is length, and then modify the function call to provide a second argument. Run the program again.
- Next modify the main program to call the input function to get a length from the user, assigning to a variable. Use this length as the argument to the function invocation to square.
- The functions lt and rt make 90-degree turns by default, but you can provide a second argument that specifies the number of degrees to turn. For example, lt(bob, 45) turns bob 45 degrees to the left.
Make a copy of square and change the name to polygon. Add another parameter named n and modify the body so it draws an n-sided regular polygon. Hint: The exterior angles of an n-sided regular polygon are 360.0/n degrees
- Write a function called circle that takes a turtle, t, and radius, r, as parameters and that draws an approximate circle by invoking polygon with an appropriate length and number of sides. Test your function with a range of values of r.
Hint: First calculate the circumference given the radius. (Google if you don't remember the calculation for a circle's circumference.) Then compute length by observing that length * n should equal the circumference.
Another hint: if bob is too slow for you, you can speed him up by changing bob.delay, which is the time between moves, in seconds. bob.delay = 0.01 ought to get him moving.
- Make a more general version of circle called arc that takes an additional parameter, called angle, which determines what fraction of a circle to draw. angle is in units of degrees, so when angle=360, arc should draw a complete circle.
- Extra Credit: Write a function called moveto, that takes three parameters, t, x, and y, and whose purpose is to move turtle t from its current location to the given x, y coordinate, without drawing a line as it moves to that position. Hint: Turtle objects have additional methods called get_x() and get_y() that can retrieve the current coordinates of the turtle.
Part 2 - Additional Programs
Write a program that uses a for loop to draw 50 randomly placed circles with random colors on a graphics window. This program should be in a separate file, called circles.py, and should employ the myro or graphics module as opposed to the TurtleWorld module. It should be structured as a complete program with imports, main function definition, and conditional invocation of main. The main function should end with a getMouse() function invocation so that the window does not go away until we are ready.
Write a program named clothes.py that takes as input from the keyboard the current temperature and prints out a suggestion for clothing. If the temperature is
- 85 degrees or above, it should print "swim suit"
- between 65 and 84, inclusive, it should print "regular clothes"
- between 45 and 64, inclusive, it should print "light jacket"
- between 25 and 44, inclusive, it should print "heavy coat"
- below 25, it should print "STAY INSIDE"
Part 3 - Uploading your lab
In your assignment dropbox create a folder called hw03. Into this should be placed:- polygon.py
- circles.py
- clothes.py