CS 105 Lecture 13: Exceptions and Modules Craig Zilles (Computer - - PowerPoint PPT Presentation

cs 105
SMART_READER_LITE
LIVE PREVIEW

CS 105 Lecture 13: Exceptions and Modules Craig Zilles (Computer - - PowerPoint PPT Presentation

CS 105 Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) https://go.illinois.edu/cs105fa19 November 22, 2019 To Today 1. Excel Power Example 2. Python Warmup 3. Exceptions try/except, multiple exceptions, across


slide-1
SLIDE 1

Lecture 13: Exceptions and Modules

Craig Zilles (Computer Science) November 22, 2019 https://go.illinois.edu/cs105fa19

CS 105

slide-2
SLIDE 2

To Today

  • 1. Excel Power Example
  • 2. Python Warmup
  • 3. Exceptions
  • try/except, multiple exceptions, across functions
  • 4. Modules
  • scripts, importing, paths
  • 5. Larger Software Development
  • Stubs
  • Unit Testing

2

slide-3
SLIDE 3

Muddi Muddiest Points

ngl 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? Ya you do ya hahhahaha we FEEELING IT NOWW! WOOOOOOOOOOOO!!! JUST STAND UP AND SHOUT WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOO

3

slide-4
SLIDE 4

Ex Excel Power exampl mple

Download 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

slide-5
SLIDE 5

Wh What function should we use?

A) VLOOKUP B) INDEX C) MATCH D) SUMIFS E) COUNTIFS

5

slide-6
SLIDE 6

Wh What does test(7) re return?

def test(num): if num > 0: return True return False A) True B) False C) first True and then False D) the tuple (True, False) E) an error occurs

6

slide-7
SLIDE 7

Wh What does this code do?

prompt = 'type a number or q to quit\n' items = [] while True: item = input(prompt) if item == 'q': break items.append(float(item)) value = sum(items) / len(items) print(value)

7

slide-8
SLIDE 8

Wh Where can an exception occur?

prompt = 'type a number or q to quit\n' items = [] while True: item = input(prompt) if item == 'q': break items.append(float(item)) value = sum(items) / len(items) print(value)

8

A B C D E

slide-9
SLIDE 9

Ex Exception n Ha Handl ndling: ng: Big g Ide dea

  • 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

slide-10
SLIDE 10

Ca Can differenti tiate betw tween exce cepti tion

  • ns

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

slide-11
SLIDE 11

Ex Exceptions ns ha hande nded d up up "call cha hain" n"

  • Unhandled exceptions passed to caller

def f(): x = 1/0 print('hi') def g(): f() print('hello') g()

11

What gets printed? A) Just hi B) Just hello C) Both hi and hello D) Nothing

slide-12
SLIDE 12

An Annou

  • uce

cements ts

  • 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

slide-13
SLIDE 13

Mo Modul dules

  • 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

slide-14
SLIDE 14

useful.py de define nes s useful_function

After: import useful How 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