Py4Inf 03 Conditionals PDF
Py4Inf 03 Conditionals PDF
Chapter 3
Maintain indent to indicate the scope of the block (which lines are affected
by the if/for)
Python cares a *lot* about how far a line is indented. If you mix tabs and
spaces, you may get indentation errors even if everything looks fine
Please do this now while you are thinking about it so we can all stay sane...
This will save you
much unnecessary
pain.
increase / maintain after if or for
decrease to indicate end of block
x = 5
if x > 2 :
print 'Bigger than 2'
print 'Still bigger'
print 'Done with 2'
for i in range(5) :
print i
if i > 2 :
print 'Bigger than 2'
print 'Done with i', i
print 'All Done'
Think about begin/end blocks
x = 5
if x > 2 :
print 'Bigger than 2'
print 'Still bigger'
print 'Done with 2'
for i in range(5) :
print i
if i > 2 :
print 'Bigger than 2'
print 'Done with i', i
print 'All Done'
yes
Nested x>1
x = 42
if x > 1 : yes
x < 100
print 'More than one'
if x < 100 :
print 'Less than 100' no
print 'Less than 100'
print 'All done'
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'
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'
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'
yes
x<2 print 'small'
x = 20 no
if x < 2 : yes
print 'small' x < 10 print 'Medium'
elif x < 10 :
no
print 'Medium'
else :
print 'LARGE' print 'LARGE'
print 'All done'
All
Done
$ cat notry.py
astr = 'Hello Bob' $ python notry.py
istr = int(astr) Traceback (most recent call last):
print 'First', istr File "notry.py", line 2, in <module>
The astr = '123 istr = int(astr)ValueError: invalid literal
program istr = int(astr)
for int() with base 10: 'Hello Bob'
stops print 'Second', istr
here
All
Done
Generic
Software Computer
Input
Central
Devices
Processing
Unit
Secondary
Memory
Output Main
Devices Memory
$ cat tryexcept.py
When the first conversion fails - it
astr = 'Hello Bob'
just drops into the except: clause
try:
and the program continues.
istr = int(astr)
except:
istr = -1
$ python tryexcept.py
print 'First', istr
First -1
Second 123
astr = '123'
try:
istr = int(astr)
except: When the second conversion
istr = -1 succeeds - it just skips the except:
clause and the program continues.
print 'Second', istr
try / except astr = 'Bob'
print 'Hello'
astr = 'Bob'
try: istr = int(astr)
print 'Hello'
istr = int(astr)
print 'There' print 'There'
except:
istr = -1
istr = -1
print 'Done', istr
print 'Done', istr Safety net
Sample try / except
rawstr = raw_input('Enter a number:')
try:
ival = int(rawstr)
except: $ python trynum.py
ival = -1 Enter a number:42
Nice work
if ival > 0 : $ python trynum.py
print 'Nice work' Enter a number:forty-two
else: Not a number
print 'Not a number' $
Exercise
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
475 = 40 * 10 + 5 * 15
Exercise
Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input