Difference between revisions of "CSC111 Lab 10 2014"
(→TypeError Exceptions) |
(→TypeError Exceptions) |
||
Line 8: | Line 8: | ||
==TypeError Exceptions== | ==TypeError Exceptions== | ||
<br /> | <br /> | ||
− | The code section below | + | The code section below will be our testbed for this part of the lab. You have to make it compute the different values we are interested in '''without using if/else/elif statements,''' using instead '''try/except''' statements. |
<br /> | <br /> | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | + | # happiness.py | |
− | + | # Lab 10 demo | |
− | + | ||
− | + | SWsFriends = [ | |
− | + | # name pocket-money debt | |
− | + | ("Sleepy", 10, "NA" ), | |
− | + | ("Sneezy", 25.50, 0 ), | |
− | + | ("Bashful", 0.50, 10.51 ), | |
− | + | ( "Happy", 0, 0 ), | |
− | + | ( "Grumpy", 0, 100.30 ), | |
− | + | ( "Dopey", "NA", 0 ), | |
− | + | ( "Doc", 200, 0 ) ] | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
def main(): | def main(): | ||
− | for | + | for name, pocket, debt in SWsFriends: |
− | print( " | + | print( name, "has", pocket, "dollar(s) in his pocket, and owes", |
− | + | debt, "dollar(s)" ) | |
− | |||
main() | main() | ||
− | |||
</source> | </source> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
* Run the program. | * Run the program. | ||
− | * Notice that | + | * Notice that some amounts of money have "NA" as a value (Not Available). |
− | * | + | * Add a for-loop to your program that will add up the total amount of money Snow White's friends have in their pocket. |
+ | Do not anticipate errors. <font color="magenta">Just go for it!</font> Assume there won't be errors and put all the code necessary to compute the sum of all the numbers in the 2nd column. | ||
− | + | * When you run your program the first time, you will get a <font color="red">TypeError</font> because you cannot take the '''float( )''' of the string '''"NA"'''. Add a '''try/except''' clause around the addition, as illustrated below: | |
− | |||
− | |||
− | |||
<br /> | <br /> | ||
+ | <source lang="python"> | ||
+ | try: | ||
+ | sumPocket += pocket | ||
+ | except TypeError: | ||
+ | continue | ||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | * Run your program again. Verify that you get the total sum in all the pockets (we assume that NA will be equivalent to 0). | ||
+ | |||
+ | <font color="blue">total sum in all the pockets: 236.0</font> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
{| style="width:100%; background:silver" | {| style="width:100%; background:silver" | ||
|- | |- | ||
| | | | ||
− | + | ==Challenge 1== | |
|} | |} | ||
[[Image:QuestionMark1.jpg|right|120px]] | [[Image:QuestionMark1.jpg|right|120px]] | ||
<br /> | <br /> | ||
− | * | + | * Add a new loop that computes the total amount of debts held by the 7 friends. |
− | + | * Do not use if statements. Use a new try/except clause to protect your code from exceptions. | |
− | * | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 87: | Line 73: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | |||
+ | ==Challenge 2== | ||
+ | |} | ||
+ | [[Image:QuestionMark3.jpg|right|120px]] | ||
<br /> | <br /> | ||
− | + | * Make your program print the contents of the list in 3 columns, as shown below: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<br /> | <br /> | ||
− | + | <source lang="text"> | |
− | + | Sleepy 10 NA | |
− | + | Sneezy 25.5 0 | |
− | + | Bashful 0.5 10.51 | |
− | + | Happy 0 0 | |
− | + | Grumpy 0 100.3 | |
− | + | Dopey NA 0 | |
− | + | Doc 200 0 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</source> | </source> | ||
<br /> | <br /> | ||
− | |||
− | |||
<br /> | <br /> | ||
Revision as of 17:56, 7 April 2014
--D. Thiebaut (talk) 13:39, 7 April 2014 (EDT)
Contents
Exceptions
If you feel you have a good handle on exceptions, then move on to the next section. Otherwise do the exercises of this section.
TypeError Exceptions
The code section below will be our testbed for this part of the lab. You have to make it compute the different values we are interested in without using if/else/elif statements, using instead try/except statements.
# happiness.py
# Lab 10 demo
SWsFriends = [
# name pocket-money debt
("Sleepy", 10, "NA" ),
("Sneezy", 25.50, 0 ),
("Bashful", 0.50, 10.51 ),
( "Happy", 0, 0 ),
( "Grumpy", 0, 100.30 ),
( "Dopey", "NA", 0 ),
( "Doc", 200, 0 ) ]
def main():
for name, pocket, debt in SWsFriends:
print( name, "has", pocket, "dollar(s) in his pocket, and owes",
debt, "dollar(s)" )
main()
- Run the program.
- Notice that some amounts of money have "NA" as a value (Not Available).
- Add a for-loop to your program that will add up the total amount of money Snow White's friends have in their pocket.
Do not anticipate errors. Just go for it! Assume there won't be errors and put all the code necessary to compute the sum of all the numbers in the 2nd column.
- When you run your program the first time, you will get a TypeError because you cannot take the float( ) of the string "NA". Add a try/except clause around the addition, as illustrated below:
try:
sumPocket += pocket
except TypeError:
continue
- Run your program again. Verify that you get the total sum in all the pockets (we assume that NA will be equivalent to 0).
total sum in all the pockets: 236.0
Challenge 1 |
- Add a new loop that computes the total amount of debts held by the 7 friends.
- Do not use if statements. Use a new try/except clause to protect your code from exceptions.
Challenge 2 |
- Make your program print the contents of the list in 3 columns, as shown below:
Sleepy 10 NA
Sneezy 25.5 0
Bashful 0.5 10.51
Happy 0 0
Grumpy 0 100.3
Dopey NA 0
Doc 200 0
Printing an Error Message for the User
Other Exceptions
- Imagine that now the list statistics is defined as shown below, and that for some countries the ISP value (the last one) is missing:
statistics = [['Afghanistan', 28710000, 'NA', 'NA', 1], ['Albania', 3580000,
12000, 'NA', 10], ['Algeria', 32810000, 180000, 'NA'],
['Andorra', 69150, 24500, 'NA', 1], ['Angola', 10760000,
60000, 'NA', 1], ['Anguilla', 12738, 919, 'NA', 16],
['Antigua_and_Barbuda', 67897, 5000, 'NA', 16], ['Argentina',
38740000, 4650000, 'NA', 33], ['Armenia', 3320000, 30000, 'NA',
9], ['Aruba', 70844, 24000, 'NA' ], ['Australia', 19730000,
13010000, 9020000, 571], ['Austria', 8180000, 4650000,
1300000, 37], ['Azerbaijan', 7830000, 25000, 'NA'],
['Bahrain', 667238, 140200, 'NA', 1], ['Bangladesh',
138440000, 150000, 'NA', 10], ['Barbados', 277264, 6000, 'NA',
19], ['Belarus', 10330000, 422000, 'NA', 23], ['Belgium',
10280000, 4870000, 1600000, 61], ['Belize', 266440, 18000,
'NA', 2], ['Benin', 7040000, 25000, 'NA', 4], ['Bhutan',
2130000, 2500, 'NA', 'NA'], ['Bolivia', 8580000, 78000, 'NA', 9],
['Bosnia_and_Herzegovian', 3980000, 45000, 'NA', 3],
['Botswana', 1570000, 33000, 'NA', 11], ['Brazil', 182030000,
22320000, 10860000, 50], ['Brunei', 358098, 35000, 'NA', 2],
['Bulgaria', 7530000, 1610000, 'NA', 200], ['Burkina_Faso',
13220000, 25000, 'NA'], ['Burma', 42510000, 10000, 'NA', 1],
['Burundi', 6090000, 6000, 'NA', 1], ['Cambodia', 13120000,
10000, 'NA', 2], ['Cameroon', 15740000, 45000, 'NA', 1] ]
- Write a program (or copy/paste part of the previous one) that will read the list above, and output the total population (which should be the same as before).
- For this you should write your program as if all the country lists contained 5 quantities.
- Then you run your program.
- You observe that it crashes when it tries to extract the 5th quantity from a country list.
- You note the name of the error generated (which is on the last line, and usually of the form XxxxError, such as IndexError, TypeError, NameError, ValueError, etc.)
- Add a try/except statement around the code that generates the error (Python refers to a run-time error that creates a crash as an exception):
try:
country, pop, users, active, isp = someVariableOfYours
except XxxxError:
country, pop, users, active = someVariableOfYours
- Run your program again and observe that it doesn't crash and that it reports the correct information.