1 computer science of graphics and games mont 105s, spring 2009 session 4 conditionals turtles
TRANSCRIPT
1
Computer Science of Graphics and Games
MONT 105S, Spring 2009Session 4ConditionalsTurtles
2
General Form for a Conditional
if condition: # condition is true or falsePython code A # Executed if condition is true
else:Python code B # Executed if condition is false
Only one of the two blocks of code is executed.
3
General Form for using elif in a Conditional
if condition1: # condition is true or falsePython code A # Executed if condition1 is true
elif condition2:Python code B #Executed if condition1 is false and 2 is true
else:Python code C # Executed if conditions 1 and 2 are false
Only one of the blocks of code is executed.
4
Example Decision Tree
"Enter your height: "
print "Tall" height >= 70?
print "Above average"
true false
print "Short"
height >= 75?
true false
5
An Alternate Decision Treefor if..elif..else
"Enter your height: "
print "Tall" print "Above average" print "Short"
height >= 75 height >= 70Otherwise(height < 70)
Conditions are printed next to the branches.Paths are tested one at a time from left to right.First condition that is true is the path that is followed.Only one path is executed.
6
Program Example
# Program height.py
height = input("Enter your height in inches: ")if height >= 75: print "Wow! You are tall!" print "Please join our basketball team."elif height >= 70: print "You are above average height."else: print "You are shorter than average."print "Thank you for participating in our survey."
7
Multiple decisions
"Do you like to ski?"
"Downhill or cross country?" "You should learn!"
true false
true false
"Try Wachusett" "Try the local trails"
In Python, we use a nested conditional to program this decision tree.
skiAns equals "yes"?
response equals "downhill"?
8
# Program: downhill.py
skiAns = raw_input("Do you like to ski? ")if skiAns == "yes": response = raw_input("Downhill or cross country? ") if response == "downhill": print "Try Wachusett!" else: print "Try the local trails."else: print "You should learn!"
Nested Conditional Example
9
Recall Classes and Objects
Python is an object oriented programming language.All items of data are objects.
Objects have properties: Things that describe the objectsObjects have methods: Things an object can do.
A class describes a group of objects that have the same methods and set of properties.
Example: A car classProperties: color, number of doors, body typeMethods: accelerate, brake, steer
My car is an object that is a member of the car class.
10
The Turtle Class
A turtle in python is an object from the Turtle class.
A turtle can move around a graphics window.
A turtle has a pen that can be up or down. If the pen is down, the turtle draws as it moves.
Turtle properties:x positiony positiondirection (that it's facing)pen position (up or down)pen color
11
Turtle Methods
Turtle methods:
forward(distance) Move forward <distance> pixelsbackward(distance) Move backward <distance> pixelsright(angle) Turn right <angle> degreesleft(angle) Turn left <angle> degreesup( ) Lift pen updown( ) Put Pen downgoto(x, y) Move to position x, ycircle(radius) Draw a circle with a given radius.
12
Turtle Example#Use a turtle to draw a triangle.
from turtle import Pen as Turtle #import from library
yertle = Turtle( ) #Create a new Turtle object; name it yertleyertle.forward(100) #Move yertle forward by 100 pixelsyertle.left(120) #Turn yertle left by 120 degreesyertle.forward(100) #Move yertle forward by 100 pixelsyertle.left(120) #Turn yertle left by 120 degreesyertle.forward(100) #Move yertle forward by 100 pixelsyertle.left(120) #Turn yertle left by 120 degrees