Download Session 3 - Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Science of
Graphics and Games
MONT 105S, Spring 2009
Session 3
Decision Trees
Conditionals
1
Sequential Programming
# Program: winter.py
answer = raw_input("Do you like to ski? ")
print "You said " + answer
print "Great!"
In sequential programming, each statement is executed in order.
The program runs the same way every time it is run.
"Do you like to ski?"
print "You said " + answer
print "Great!"
2
Decision Trees
We would like to be able to respond to the user's input,
giving different responses depending on what she enters:
"Do you like to ski?"
answer equals yes?
true
print "You will love winter here!"
false
print "You should learn!"
Decision trees diagram the different paths a program can
take depending on a series of questions and answers.
3
Conditional Statements
# Program: skiReport.py
answer = raw_input("Do you like to ski? ")
if answer == "yes":
print "You will love winter here!"
else:
print "Better learn!"
4
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.
5
Conditions
Conditions are expressions that have a value of true or false.
A common type of condition is a comparison.
Example of comparisons:
x>y
myName == "Jessica"
14 < size
What are the variables in the above examples?
Also can use, <, <=, >=, or != for comparisons.
6
Program Example
# Program compare.py
print "Type in two integers."
x = input("First integer: ")
What is the output
y = input("Second integer: ")
if the user enters 4
if x > y:
and then 7?
print x, "is greater than", y
else:
print y, "is greater than or equal to", x
7
Compound statements
You can have as many separate statements as you like in the if
or the else clauses. The statements must all have the same
indentation. Together they form a block or a compound
statement.
answer = raw_input("Do you like to ski? ")
if answer == "yes":
print "You will love winter here!"
print "We have lots of snow!"
else:
print "Better learn!"
print "You will enjoy winter more!"
print "Goodbye!"
8
Three way choices
"Guess a Number: "
guess < prizeNumber?
true
false
print "Too low"
guess > prizeNumber?
true
print "Too high"
false
print "You win!"
9
Using if .. elif .. else
prizeNumber = 42
guess = input("Guess a number ")
if guess < prizeNumber:
print "Too low."
elif guess > prizeNumber:
print "Too high."
else:
print "You win!"
Only one of the
three blocks is
executed.
10
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.
11
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!"
12