Session 4 - Computer Science

Download Report

Transcript Session 4 - Computer Science

Computer Science of
Graphics and Games
MONT 105S, Spring 2009
Session 4
Conditionals
Turtles
1
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.
2
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.
3
Example Decision Tree
"Enter your height: "
height >= 75?
true
false
height >= 70?
print "Tall"
true
print "Above average"
false
print "Short"
4
An Alternate Decision Tree
for if..elif..else
"Enter your height: "
height >= 75
print "Tall"
height >= 70
print "Above average"
Otherwise
(height < 70)
print "Short"
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.
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."
else:
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 position
y position
direction (that it's facing)
pen position (up or down)
pen color
10
Turtle Methods
Turtle methods:
forward(distance)
backward(distance)
right(angle)
left(angle)
up( )
down( )
goto(x, y)
circle(radius)
Move forward <distance> pixels
Move backward <distance> pixels
Turn right <angle> degrees
Turn left <angle> degrees
Lift pen up
Put Pen down
Move to position x, y
Draw a circle with a given radius.
11
Turtle Example
#Use a turtle to draw a triangle.
from turtle import Pen as Turtle
yertle = Turtle( )
yertle.forward(100)
yertle.left(120)
yertle.forward(100)
yertle.left(120)
yertle.forward(100)
yertle.left(120)
#import from library
#Create a new Turtle object; name it yertle
#Move yertle forward by 100 pixels
#Turn yertle left by 120 degrees
#Move yertle forward by 100 pixels
#Turn yertle left by 120 degrees
#Move yertle forward by 100 pixels
#Turn yertle left by 120 degrees
12