1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals
2 Sequential Programming # Program: winter.py answer = raw_input("Do you like to ski? ") print "You said " + answer print "Great!" "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.
3 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?" print "You will love winter here!"print "You should learn!" truefalse Decision trees diagram the different paths a program can take depending on a series of questions and answers. answer equals yes?
4 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!"
5 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.
6 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.
7 Program Example # Program compare.py print "Type in two integers." x = input("First integer: ") y = input("Second integer: ") if x > y: print x, "is greater than", y else: print y, "is greater than or equal to", x What is the output if the user enters 4 and then 7?
8 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!"
9 Three way choices "Guess a Number: " print "Too low"guess > prizeNumber? print "Too high" truefalse print "You win!" guess < prizeNumber? true false
10 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.
11 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"?
12 # 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