Download presentation
Presentation is loading. Please wait.
1
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Jehan-François Pâris Fall 2016 1
2
THE ONLINE BOOK CHAPTER IV TURTLES
3
Turtles? A Python package allowing novice users to draw simple colorful figures Python port of the Logo turtle Seymour Pappert Used to taech programming concepts yo young children
4
Starting up (I) Import the turtles package import turtle
Create a drawing canvas mycanvas = turtle.Screen() A new instance of the Screen object as defined in the turtle package
5
Starting up (II) Create a turtle Alice = turtle.Turtle()
A new instance of the Turtle object as defined in the turtle package Turtle and turtle are different identifiers Python is case-sensitive Can have many turtles on the same canvas Move the turtle
6
Example import turtle wn = turtle.Screen() # Creates a canvas
alex = turtle.Turtle() # Create a turtle "alex" alex.forward(50) # Go forward 50 units alex.left(90) # Turn by 90 degrees alex.forward(30) # Go forward 50 units wn.mainloop() # Wait for user to close window
7
Another example import turtle wn = turtle.Screen()
shasta = turtle.Turtle() shasta.right(90) shasta.forward(80) shasta.left(90) shasta.forward(40) wn.mainloop()
8
Allowing drawing discontinuities
shasta.penup() Tell the turtle shasta to start moving without leaving a trace Think of asking shasta to raise up its pen shasta.pendown() Tell the turtle shasta to return to its normal state and leave a trace Think of asking shasta to put its pen down to the paper
9
Guess what this program draws
import turtle wn = turtle.Screen() shasta = turtle.Turtle() shasta.right(90) shasta.forward(80) shasta.left(90) shasta.forward(40) shasta.penup() shasta.pendown() shasta.forward(80) shasta.penup() shasta.backward(40) shasta.left(90) shasta.forward(40) shasta.backward(80) wn.mainloop()
10
The answer
11
Do exercises in section 4.2
Teaches you to put things in the right order
12
Making it look better Can Add color to canvas wn.bgcolor("lightgreen")
Specify color and thickness of he pen of a given turtle tess = turtle.Turtle() tess.color("blue") tess.pensize(3)
13
Another way to exit wn.exitonclick()
Closes the canvas window when the user clicks his or her mouse
14
Can have multiple turtles
alice = turtle.Turtle() bob = turtle.Turtle() … Each with its color and pensize attributes alice.color("blue") bob.color("red") ...
15
A small problem Want to congratulate several students for their project print("Congratulations, Alice!") print("Congratulations, Bob!") print("Congratulations, Carol!") … Becomes quickly tedious
16
The solution The for loop for x in ["Alice", "Bob", "Carol"] :
print("Congratulations, " + x +"!") Repeats the statement(s) inside the loop for all entries in the list ["Alice", "Bob", "Carol"] assigning in turn the value of each entry to x
17
What are lists? A truly wonderful construct of Python
Extends the C/C++/Java/C# concept of array I love it and you will! Sequence of values inside square brackets separated by commas ["Alice", "Bob", "Carol"] [0, 1, 2, 3, 4, 5] [1, 3, 5, 7, 13, 17, 23]
18
The for loop syntax for iterator in list : body of the list . . .
rest of the program
19
Please note For statements end with a column Do not forget it!
All statements that will be repeated Are said to be inside the loop Must all be indented by the same numbers of spaces or tabs Must use them both in a consistent fashion Python is very very finicky
20
Example (I) for x in ["Alice", "Bob", "Carol"] :
print("Congratulations, " + x +"!") print("You did very well!") Will print two lines of congratulations for all three entries in the list Total of six lines
21
Example (II) for x in ["Alice", "Bob", "Carol"] :
print("Congratulations, " + x +"!") print("You did very well!") #outside Will print a single line of congratulations for all three entries in the list then You did very well! only once Total of four lines
22
Example (III) for x in ["Alice", "Bob", "Carol"] :
print("Congratulations, " + x +"!") print("You did very well!") #outside Will not print anything because the interpreter will refuse to execute the program
23
Warning When you move pieces of code around in your programs
Very easy to mess with the indentations in your code Python is unforgiving Must check and recheck your indentations
24
The good for index in list : loop body
Indentations make your code more concise and more legible Compare for index in list : loop body with for (weird construct) { loop body }
25
The bad Python is unforgiving
26
Application: drawing a square
import turtle wn = turtle.Screen() alex = turtle.Turtle() for i in [0, 1, 2, 3]: # repeat 4 times alex.forward(50) alex.left(90) wn.exitonclick()
27
Comments We could have used for i in [1, 2, 3, 4]:
as the values of i are not used inside the loop It is just a convention to start all iterations with the zero value Unless you have a reason to do otherwise Shared with C/C++/Java/C#/Ruby and many other similar languages Get used to it!
28
Back to Babylonian math
""" Computes square roots """ number=float(input("Enter n >= 0: ")) root = number/2 for i in [0, 1, 2, 3, 4] : inverse = number/root root = (root + inverse)/2 print ("Its root is", root)
29
A problem What if we need more precision?
Must specify more iterations: for i in [0, 1, 2, 3, 4, 5, 8] : Becomes quickly tedious
30
The solution range(n) n is not in the range!
Lets index go through list of n values Starting at 0 Ending at n – 1 n is not in the range! Because we like to start at zero
31
A better algorithm """ Computes square roots """
number=float(input("Enter n >= 0: ")) root = number/2 for i in range(10) : inverse = number/root root = (root + inverse)/2 print ("Its root is", root)
32
Its output Enter a positive number: 4096 Its root is 1025.0
33
More turtle tricks (I) alex.shape("turtle")
Can change the shape of the turtle alex.shape("turtle") Options include "arrow", "blank", "circle", "classic", "square", "triangle", and "turtle"
34
More turtle tricks (II)
Can change the speed of the turtle alex.speed(5) 1 is slowest and 10 is fastest Can ask the turtle to stamp a mark at its current location alex.stamp()
35
A nice turtle program (I)
import turtle wn = turtle.Screen() wn.bgcolor("lightgreen") tess = turtle.Turtle() tess.shape("turtle") tess.color("blue")
36
A nice turtle program (II)
tess.penup() size = 20 for i in range(30): tess.stamp() # Leave a mark size = size # Increase size tess.forward(size) # Move along tess.right(24) # Turn her 24o wn.mainloop()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.