0% found this document useful (0 votes)
4 views19 pages

3.2 More Conditional Statements

The document discusses various conditional execution patterns in Python, including one-way, two-way, and multi-way decisions using if, elif, and else statements. It also introduces the try/except structure for error handling in code. Additionally, it provides exercises for implementing pay computation and handling non-numeric input gracefully.

Uploaded by

kawsik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

3.2 More Conditional Statements

The document discusses various conditional execution patterns in Python, including one-way, two-way, and multi-way decisions using if, elif, and else statements. It also introduces the try/except structure for error handling in code. Additionally, it provides exercises for implementing pay computation and handling non-numeric input gracefully.

Uploaded by

kawsik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Conditional – Part 2

PYTHON FOR
EVERYBODY

More Conditional Execution Patterns


Conditional – Part 2
PYTHON FOR
EVERYBODY

Visualize Blocks X=4

no yes
x = 4 x>2

if x > 2 :
print('Not bigger') print('Bigger')
print('Bigger')
else :
print('Smaller')

print 'All done' print 'All Done'


Conditional – Part 2
PYTHON FOR
EVERYBODY

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

The try / except Structure


• You surround a dangerous section of code with try and except
• If the code in the try works - the except is skipped
• If the code in the try fails - it jumps to the except section5
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

astr = 'Hello Bob' When the first conversion fails - it


try: just drops into the except: clause
istr = int(astr) and the program continues.
except:
istr = -1
$ python tryexcept.py
print('First', istr) First -1
Second 123
astr = '123'
try:
istr = int(astr)
except:
istr = -1 When the second conversion
succeeds - it just skips the except:
print('Second', istr) clause and the program continues.
Conditional – Part 2
PYTHON FOR
EVERYBODY

try / except astr = 'Bob'

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)

print('Done', istr) Safety net


Conditional – Part 2
PYTHON FOR
EVERYBODY

Sample try / except


rawstr = input('Enter a number:')
try:
ival = int(rawstr) $ python3 trynum.py
except: Enter a number:42
ival = -1 Nice work
$ python3 trynum.py
if ival > 0 : Enter a number:forty-two
print('Nice work') Not a number
else: $
print('Not a number')
Conditional – Part 2
PYTHON FOR
EVERYBODY

Exercise

Rewrite your pay computation to give the


employee 1.5 times the hourly rate for hours
worked above 40 hours.

Enter Hours: 45
Enter Rate: 10

Pay: 475.0
475 = 40 * 10 + 5 * 15
Conditional – Part 2
PYTHON FOR
EVERYBODY

Exercise

Rewrite your pay program using try and except so


that your program handles non-numeric input
gracefully.

Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input

Enter Hours: forty


Error, please enter numeric input
Conditional – Part 2
PYTHON FOR
EVERYBODY

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.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors and Translators here

You might also like