Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 4 Conditionals Turtles.

Similar presentations


Presentation on theme: "1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 4 Conditionals Turtles."— Presentation transcript:

1 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 4 Conditionals Turtles

2 2 General Form for a Conditional if condition:# condition is true or false Python 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 3 General Form for using elif in a Conditional if condition1:# condition is true or false Python 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 4 Example Decision Tree "Enter your height: " print "Tall" height >= 70? print "Above average" truefalse print "Short" height >= 75? true false

5 5 An Alternate Decision Tree for if..elif..else "Enter your height: " print "Tall" print "Above average" print "Short" height >= 75height >= 70 Otherwise (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 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 7 Multiple decisions "Do you like to ski?" "Downhill or cross country?""You should learn!" true false truefalse "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 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 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 objects Objects 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 class Properties: color, number of doors, body type Methods: accelerate, brake, steer My car is an object that is a member of the car class.

10 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 position y position direction (that it's facing) pen position (up or down) pen color

11 11 Turtle Methods Turtle methods: forward(distance)Move forward pixels backward(distance)Move backward pixels right(angle)Turn right degrees left(angle)Turn left degrees up( )Lift pen up down( )Put Pen down goto(x, y)Move to position x, y circle(radius)Draw a circle with a given radius.

12 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 yertle yertle.forward(100)#Move yertle forward by 100 pixels yertle.left(120)#Turn yertle left by 120 degrees yertle.forward(100)#Move yertle forward by 100 pixels yertle.left(120)#Turn yertle left by 120 degrees yertle.forward(100)#Move yertle forward by 100 pixels yertle.left(120)#Turn yertle left by 120 degrees


Download ppt "1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 4 Conditionals Turtles."

Similar presentations


Ads by Google