Download presentation
Presentation is loading. Please wait.
Published byValentine Hamilton Modified over 6 years ago
1
4. If Statements Let's Learn Python and Pygame
Aj. Andrew Davison, CoE, PSU Hat Yai Campus 4. If Statements Have your program make choices. Depending on a test have a program do different things.
2
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.
3
"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...
4
age1.py
5
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)
6
Flowchart executing program Test: no age > 16? yes
Action: print old execution continues...
7
Test Operations
8
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
9
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")
10
Flowchart executing program Test: no age > 16? yes
Action: print young Action: print old execution continues...
11
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"
12
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, ...
13
age2.py
14
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, ...
15
age3.py
16
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, ...
17
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
18
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?
19
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?
20
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?
21
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?
22
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?
23
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?
24
Summary of or, and or: True if one or both tests are true
and: True only if both tests are true
25
age4.py and: True only if both tests are true
26
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.