Lecture 13: Exceptions and Modules
Craig Zilles (Computer Science)
November 22, 2019
https://go.illinois.edu/cs105fa19
CS 105
Today1. Excel Power Example2. Python Warmup3. Exceptions
• try/except, multiple exceptions, across functions4. Modules
• scripts, importing, paths5. Larger Software Development
• Stubs• Unit Testing
2
Muddiest Pointsngl lookin pretty good rn feeling pretty good rn hope this finds you well i mean cmon i know its probably a little boring reading like 500 of these but I want you to GET PUMPED UP WOOOOOOOOOOOOOOOOO!!!!!!!!!! CAN YOU FEEL IT!!! CAN YOU!??? alright lets uhh simmer down just a little ok i got a little excited there but you know what i hope you got a little excited right there too yknow? I hope you can feel the energy im putting into this text yknow? cmooooon you feel it, ya? Yayou do ya hahhahaha we FEEELING IT NOWW! WOOOOOOOOOOOO!!! JUST STAND UP AND SHOUT
WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
3
Excel Power exampleDownload the following CSV file which contains the number of fishing licenses sold in each county each year from 2005 to 2012 for the state of Illinois. Open it up and compute the total number of RES FISHING licenses sold in McHenry county in the years from 2005 to 2006 (inclusive).
What if we wanted to compute this for all licenses and all counties?
4
What function should we use?
A) VLOOKUPB) INDEXC) MATCHD) SUMIFSE) COUNTIFS
5
What does test(7) return?def test(num):
if num > 0:return True
return False
A) TrueB) FalseC) first True and then FalseD) the tuple (True, False)E) an error occurs
6
What does this code do?prompt = 'type a number or q to quit\n'items = []while True:
item = input(prompt)if item == 'q':
breakitems.append(float(item))
value = sum(items) / len(items)print(value)
7
Where can an exception occur?prompt = 'type a number or q to quit\n'items = []while True:
item = input(prompt)if item == 'q':
breakitems.append(float(item))
value = sum(items) / len(items)print(value)
8
A
B
C
D
E
Exception Handling: Big Idea• Don't crash the program; gracefully handle user errors• Do it in a way that doesn't encumber the normal path
• Exception handling (like objects) is complexity management
try:# do potentially bad things
except:# handle bad things if they happen
9
Can differentiate between exceptions try:
# do potentially bad things
except ValueException as e:# handle ValueExceptions# e is bound to exception object
except ZeroDivisionError:# ...
except:# handle any other exceptions
10
Exceptions handed up "call chain"• Unhandled exceptions passed to caller
def f():x = 1/0print('hi')
def g():f()print('hello')
g()11
What gets printed?
A) Just hiB) Just helloC) Both hi and helloD) Nothing
Annoucements• 5% curve on Exam 2, 10% curve on Exam 3• Clicker points capped at 150• Lab after break: Data Analysis• Quiz 4: 12/5 - 12/7
• Mix of Excel and Python content• Final Exam
• Almost all Python (can't run Excel in CBTF)• Hiring Course Assistants for Spring: (see mass mail)
• https://forms.gle/XqHiiphAYYJRXUHr8
12
Modules• One of the main uses of computing is automation
• Often you want to do similar things repeatedly
• Write a piece of code once and use it again and again• Put the code into a file (e.g., useful.py) • Import that file as a module
• import useful• useful.py must be in Python's path
• Path is set of directories where Python will look for file• Most easily done by running Python where the file is
13
useful.py defines useful_function
After:import usefulHow to call useful_function with "hi" as argument
A) function("hi")B) useful_function("hi")C) useful.function("hi")D) useful.useful_function("hi")E) None of the above
14