Due: Tuesday, February 14, 2017, 11:59 pm
We'll spend some more time this week learning Python syntax and creating functions, which are collections of code that we may want to execute more than once. An important aspect of functions are parameters, which are a way of making a function do more than one thing.
In addition, we'll focus on overall code organization. How you write and organize your code makes a big difference in how easy it is to write, modify, debug, and re-use. For example, we will be grouping functions that do similar things in the same file, we will have only one copy of each function, and we will be adding comments to our code. Good code organization saves you time and effort.
Comments are important in code. Comments have three purposes.
From now on we'll be grading your code not only on its functionality, but also on whether it is well-commented and easy to read. Well-commented code has a comment (1 sentence) at the beginning of each function describing what it does and the meaning of its parameters. It will also generally have a comment before each major section of the function.
The comment at the beginning of each function is special. We call it a docstring and use different characters to set it apart from the code. We use three double-quotes at the beginning and at the end. Below is an example of a file with the commenting conventions you need to use for all of your projects.
# Ying Li # 02/08/2017 # simple_turtle.py - uses the turtle to draw a triangle import turtle def draw_triangle( edge_len ): """ Draw an equilateral triangle Each edge should be edge_len long """ turtle.forward( edge_len ) turtle.left( 120 ) turtle.forward( edge_len ) turtle.left( 120 ) turtle.forward( edge_len ) turtle.left( 120 ) # main code draw_triangle( 50 ) draw_triangle( 100 ) raw_input( "Press enter when ready " )
From now on, we'll be importing packages using the above syntax. It requires you to access the turtle functions slightly differently, but it enforces better code organization and style, in addition to reducing unintended errors.
turtle.reset() turtle.forward( 100 ) raw_input('Press enter to continue')
The above code is pretty much identical to how we started last week,
but since we included the turtle package using the
turtle syntax, we have to use the prefix
turtle. prior to any function from the turtle
Recall that to execute the file in a terminal you need to change the
working directory to the one with your file in it by typing
cd and then the path to the directory. You can either
type the path to your Project2 directory in Terminal or you can use
the Finder to drag and drop the path to your project folder into
Terminal after you type cd and a space.
To run your program, type:
Function syntax reminders:
The goto function should raise the pen using the
turtle.up() function, use the
function to jump to the location specified by the parameters, and
then put down the pen using
While you are still learning to interpret code, it may also be useful to have print statements in your functions that indicate what is happening. Print statements are your most important debugging tool, because you can use them to find out information about both the order of operations and the value of parameters at different times during the process. At the beginning of your goto function, put the following print statement.
print 'goto(): going to', x, y
Once you have written the function, test it by using the goto function and drawing some lines. For example, after the function definition, but before the raw_input() call, you could do the following.
print 'running main code' goto( 100, 100 ) turtle.forward( 50 ) goto( -100, 50 ) turtle.forward( 50 )
Note the difference between the function
turtle.goto(). Without the turtle prefix, python will
call the function you defined. With the turtle prefix, python will
call the goto function in the turtle package.
It's also important to start thinking about where functions and top-level code are placed relative to each other in your file. Top-level code should always come after all function definitions. If you intersperse code and functions, it will lead to confusion and be difficult to debug or modify.
Execute your code and make sure your goto function works properly. Note what it prints out and make sure you understand the order of operation.
Copy and paste the following code template into your file. Then select all of the text in your file (cmd-a) and select the Entab option under the Text menu. This makes sure all of your white space uses tabs. Alternatively you could use the Detab option, which makes all of your white space spaces.
def block( x, y, width, height ): """ Draws a rectangle of the given width and height with lower left corner at location (x,y) """ goto( x, y ) print 'block(): drawing block of size', width, height # tell the turtle to go forward by width # tell the turtle to turn left by 90 degrees # tell the turtle to go forward by height # tell the turtle to turn left by 90 degrees # repeat the above 4 commands
Remove your old main code (the goto and turtle.forward commands) and replace them with a couple of calls to the block function.
The first command in the function should be a print statement. The remainder of the function should call the block function 2-4 times with blocks in different locations relative to the given (x, y) location. The example below shows the function definition, the print statement, and three calls to the block function. Note the use of the scale factor in both the location offsets and the size of the block.
def bunchOfBlocks( x, y, scale ): """ <add a docstring here> """ print 'bunchOfBlocks(): drawing blocks at location', x, y # put several calls to the block function here block( x, y, 45*scale, 90*scale ) block( x+15*scale, y+90*scale, 15*scale, 30*scale ) block( x+20*scale, y+120*scale, 5*scale, 15*scale )
The picture below is result of three calls to the bunchOfBlocks function. Note that the relative sizes and locations of the blocks are all identical, despite different scale factors.
Make your own bunchOfBlocks function. If you want, just make some changes to the code above and see what happens. Then change the main code in your file to call the bunchOfBlocks function several times with different x, y, and scale parameters. Then run your file.
Replace the main block of code with the following. Note the use of the function random.random() in both the x, y location and scale factor. The function returns a random number between 0 and 1.0. The for expression executes the code inside the block the number of times specified in the argument to the range function.
for i in range(10): bunchOfBlocks( random.random()*600 - 300, random.random()*600 - 300, random.random()*2 + 0.5 )
When you are done with the lab exercises, you may begin Project 2.
© 2017 Ying Li. Page last modified: .