Download presentation
Presentation is loading. Please wait.
1
Survey of Computer Science CSCI 110, Spring 2011 Session 5 Conditionals Turtles
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
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
Example Decision Tree "Enter your height: " height >= 75? true
false print "Tall" height >= 70? true false print "Above average" print "Short"
5
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."
6
Multiple decisions "Do you like to ski?" skiAns equals "yes"? true
false "Downhill or cross country?" "You should learn!" response equals "downhill"? true false "Try Wachusett" "Try the local trails" In Python, we use a nested conditional to program this decision tree.
7
Nested Conditional Example
# 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." print "You should learn!"
8
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.
9
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 (horizontal) position y (vertical) position direction (that it's facing) pen position (up or down) pen color
10
Turtle Methods Turtle methods:
forward(distance) Move forward <distance> pixels backward(distance) Move backward <distance> pixels right(angle) Turn right <angle> degrees left(angle) Turn left <angle> 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.
11
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.