3.2 More Conditional Statements
3.2 More Conditional Statements
PYTHON FOR
EVERYBODY
no yes
x = 4 x>2
if x > 2 :
print('Not bigger') print('Bigger')
print('Bigger')
else :
print('Smaller')
Multi-way
yes
x<2 print('small')
no
if x < 2 :
yes
print('small')
elif x < 10 :
x < 10 print('Medium')
print('Medium') no
else :
print('LARGE') print('LARGE')
print('All done')
print('All Done')
Conditional – Part 2
PYTHON FOR
EVERYBODY
Multi-way
x=0
yes
x<2 print('small')
x = 0
no
if x < 2 :
yes
print('small')
elif x < 10 :
x < 10 print('Medium')
print('Medium') no
else :
print('LARGE') print('LARGE')
print('All done')
print('All Done')
Conditional – Part 2
PYTHON FOR
EVERYBODY
Multi-way
x=5
yes
x<2 print('small')
x = 5
no
if x < 2 :
yes
print('small')
elif x < 10 :
x < 10 print('Medium')
print('Medium') no
else :
print('LARGE') print('LARGE')
print('All done')
print('All Done')
Conditional – Part 2
PYTHON FOR
EVERYBODY
Multi-way
x = 20
yes
x<2 print('small')
x = 20
no
if x < 2 :
yes
print('small')
elif x < 10 :
x < 10 print('Medium')
print('Medium') no
else :
print('LARGE') print('LARGE')
print('All done')
print('All Done')
Conditional – Part 2
PYTHON FOR
EVERYBODY
Multi-way if x < 2 :
print('Small')
elif x < 10 :
# No Else print('Medium')
x = 5 elif x < 20 :
if x < 2 : print('Big')
print('Small') elif x < 40 :
elif x < 10 : print('Large')
print('Medium') elif x < 100:
print('Huge')
print 'All done' else :
print('Ginormous')
Conditional – Part 2
PYTHON FOR
EVERYBODY
Multi-way Puzzles
Which will never print
regardless of the value for x? if x < 2 :
print('Below 2')
if x < 2 : elif x < 20 :
print('Below 2') print('Below 20')
elif x >= 2 : elif x < 10 :
print('Two or more') print('Below 10')
else : else :
print('Something else') print('Something else')
Conditional – Part 2
PYTHON FOR
EVERYBODY
$ python3 notry.py
Traceback (most recent call last):
File "notry.py", line 2, in <module>
istr = int(astr)ValueError: invalid literal
for int() with base 10: 'Hello Bob'
$ cat notry.py
astr = 'Hello Bob' All
istr = int(astr) Done
print('First', istr)
astr = '123'
istr = int(astr)
print('Second', istr)
Conditional – Part 2
PYTHON FOR
EVERYBODY
$ python3 notry.py
Traceback (most recent call last):
File "notry.py", line 2, in <module>
The istr = int(astr)ValueError: invalid literal
program for int() with base 10: 'Hello Bob'
stops
$ cat notry.py
here astr = 'Hello Bob' All
istr = int(astr) Done
Conditional – Part 2
PYTHON FOR
EVERYBODY
Generic
Software
Computer
Input
Central
Devices
Processing
Unit
Secondary
Memory
Output Main
Devices Memory
Conditional – Part 2
PYTHON FOR
EVERYBODY
print('Hello')
astr = 'Bob'
try:
print('Hello') istr = int(astr)
istr = int(astr)
print('There')
except: print('There')
istr = -1
istr = -1
print('Done', istr)
Exercise
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
475 = 40 * 10 + 5 * 15
Conditional – Part 2
PYTHON FOR
EVERYBODY
Exercise
Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input
Summary
• Comparison operators • Nested Decisions
== <= >= > < ! =
• Multi-way decisions using elif
• Indentation
• try / except to compensate
• One-way Decisions for errors
• Two-way decisions:
if: and else:
Conditional – Part 2
PYTHON FOR
EVERYBODY
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
(www.dr-chuck.com) of the University of Michigan School of ...
Information and made available under a Creative Commons
Attribution 4.0 License. Please maintain this last slide in all
copies of the document to comply with the attribution
requirements of the license. If you make a change, feel free to
add your name and organization to the list of contributors on this
page as you republish the materials.