cs 105
play

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


  1. CS 105 Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) https://go.illinois.edu/cs105fa19 November 22, 2019

  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

  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

  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

  5. Wh What function should we use? A) VLOOKUP B) INDEX C) MATCH D) SUMIFS E) COUNTIFS 5

  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

  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

  8. Where can an exception occur? Wh prompt = 'type a number or q to quit\n' items = [] while True: item = input(prompt) A if item == 'q': B break items.append(float(item)) C value = sum(items) / len(items) D print(value) E 8

  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

  10. Ca Can differenti tiate betw tween exce cepti tion ons 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

  11. Ex Exceptions ns ha hande nded d up up "call cha hain" n" • Unhandled exceptions passed to caller def f(): What gets printed? x = 1/0 print('hi') A) Just hi B) Just hello def g(): C) Both hi and hello f() D) Nothing print('hello') g() 11

  12. An Annou ouce 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

  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

  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

Recommend


More recommend