4. If Statements Let's Learn Python and Pygame Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th 4. If Statements Have your program make choices. Depending on a test have a program do different things.
If statement: test, action(s) If Andrew got the right answer, add 1 point to his score. If Jane hit the alien, make an explosion sound. If the file isn’t there, display an error message.
"If" Flowchart If Andrew got the right answer, add 1 point to his score. executing program Test: Did Andrew get the right score? no yes Action: Add 1 point to his score execution continues...
age1.py
Code Details test: is age > 16? print("Enter your age: ", end =" ") age = int(input()) if age > 16: print("You are Old, ") print("Old, " * age) actions must be indented (add 4 spaces at the start of each line)
Flowchart executing program Test: no age > 16? yes Action: print old execution continues...
Test Operations
Comparison of Strings Comparison operators work for strings >>> "David" < "David Cameron" True >>> "Dave" < "David" >>> "Tom" < "Thomas" False >>> "Bill" < "William" >>> "AAA" < "AAB" >>> "AAA" < "aaa" >>> "aaa" < "AAA" Uses A-Z a-z ordering, letter by letter
If and Else print("Enter your age: ", end =" ") age = int(input()) if age > 16: print("You are Old, ") print("Old, " * age) else: print("You are Young")
Flowchart executing program Test: no age > 16? yes Action: print young Action: print old execution continues...
Not quite what we want! Try with 65 Many ifs No Yes grade>70 print "A" if (grade > 70): print("A") if (grade > 60): print("B") if (grade > 50): print("C") else: print("D") No Yes grade>60 print "B" No Yes grade>50 print "D" print "C"
Elifs ("else if"s) No Yes grade>70 print "A" if (grade > 70): print("A") elif (grade > 60): print("B") elif (grade > 50): print("C") else: print("D") No Yes grade>60 print "B" No Yes grade>50 print "D" print "C" Yes! Try with 65, 72, 40, ...
age2.py
age2.py Flowchart Yes! Try with 14, 7, 10, 9, ... No Yes age>16 "old" No Yes age>=12 "not bad" No Yes age>=8 "baby" "get lost" Yes! Try with 14, 7, 10, 9, ...
age3.py
age3.py Flowchart Yes! Try with 14, 7, 10, 9, ... No Yes age>16 "old" No Yes age>=12 No Yes age>14 "get lost" "not bad" "perfect" Yes! Try with 14, 7, 10, 9, ...
Complex Test Operations Operator Example Result and (2 == 3) and (-1 < 5) False or (2 == 3) or (-1 < 5) True not not (2 == 3) and: True only if both tests are true or: True if one or both tests are true not: True if test is false
1. Truth Table for or print True if one or both are true Answer? A B rate = -1 rate < 0 or rate > 100 A B A or B True False Answer?
2. Truth Table for or print True if one or both are true Answer? A B rate = 160 rate < 0 or rate > 100 A B A or B True False Answer?
3. Truth Table for or print True if one or both are true Answer? A B rate = 50 rate < 0 or rate > 100 A B A or B True False Answer?
1. Truth Table for and print True only if both are true Answer? A B rate = -1 rate >= 0 and rate <= 100 A B A and B True False Answer?
2. Truth Table for and print True only if both are true Answer? A B rate = 50 rate >= 0 and rate <= 100 A B A and B True False Answer?
3. Truth Table for and print True only if both are true Answer? A B rate = 160 rate >= 0 and rate <= 100 A B A and B True False Answer?
Summary of or, and or: True if one or both tests are true and: True only if both tests are true
age4.py and: True only if both tests are true
age4.py Flowchart and: True only if both tests are true No Yes "old" age>=10 and age <= 14 and: True only if both tests are true No Yes "get lost" "perfect