+ All Categories
Home > Documents > Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not...

Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not...

Date post: 10-Oct-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
21
Asserts and Error Handling Presentation 11
Transcript
Page 1: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Asserts and Error Handling

Presentation 11

Page 2: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Announcements for Today

Lessons

• Lesson 13, 14 for today• Videos 15.1-15.7 next time

Assignments

• Finishing Assignment 1§ We are going to score it§ Get one more chance Sun.

• Assignment 2 in progress § Will grade it by Saturday§ Solutions posted by Saturday

• Assignment 3 due next week§ Just before the exam§ Same “length” as A1

10/3/19 Asserts & Error Handling 2

• Prelim, Oct 18th 7:30-9:00§ Material up October 13th§ Study guide TOMORROW

• Conflict with Prelim time?§ Submit to Prelim 1 Conflict

assignment on CMS§ Do not submit if no conflict

Page 3: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Lab Today

• There is no lab next Tuesday/Wednesday§ Technically Wednesday is break§ We will honor this – no class§ Since no Wednesday lab, not Tuesday either

• But will hold Zoom session Tuesday!• Today’s lab is due Thursday/Friday

§ But get it out of the way soon!§ Want to focus your time on A3

10/8/19 Lists & Sequences 3

Page 4: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Assert Statements

>>> s = '1'>>> assert type(s) == int, str(s)+' is not an int'What is the error message?

10/3/19 Asserts & Error Handling 4

A: "'1' is not an int"B: "1 is not an int"C: '1' is not an intD: The error has no messageE: There is no error

Page 5: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Assert Statements

>>> s = '1'>>> assert type(s) == int, str(s)+' is not an int'What is the error message?

10/3/19 Asserts & Error Handling 5

A: "'1' is not an int"B: "1 is not an int"C: '1' is not an intD: The error has no messageE: There is no error

CORRECT

Page 6: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Assert Statements

>>> s = '1'>>> assert type(s) == str, repr(s)+' is not a string’>>> assert ' ' in s, repr(s)+' has no space'What is the error message?

10/3/19 Asserts & Error Handling 6

A: "'1' is not a string"B: "'1' has no space"C: "1 has no space"D: The error has no messageE: There is no error

Page 7: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Assert Statements

>>> s = '1'>>> assert type(s) == str, repr(s)+' is not a string’>>> assert ' ' in s, repr(s)+' has no space'What is the error message?

10/3/19 Asserts & Error Handling 7

A: "'1' is not a string"B: "'1' has no space"C: "1 has no space"D: The error has no messageE: There is no error

CORRECT

Page 8: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Brainstorm: Enforcing Preconditions

def lookup_netid(nid):"""Returns: name of student with netid nid.

Precondition: nid is a string, which consists of 2 or 3 lowercase letters followed by a sequence of digits """assert ?????

10/3/19 Asserts & Error Handling 8

Assert uses expressions only.Cannot use if-statements.

Each one must fit on one line.

Page 9: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Try-Except: Tracing Control Flow

def first(x):print('Start first.')try:

second(x)print('No errors first')

except:print('Caught first')

print('Done first')

def second(n):print('Start second.')try:

assert n <= 0, str(n)+' is not <= 0'print('No errors second')

except:print('Caught second')

assert n >= 0, str(n)+' is not >= 0'print('Ending second')

What is the output of first(1)?

10/3/19 Asserts & Error Handling 9

Page 10: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Which One is Closest to Your Answer?

A: 'Start first''Start second’AssertionError: '-1 is not <= 0'

B: 'Start first''Start second''1 is not <= 0’'Caught second' 'Done second''No errors first' 'Done first'

9/15/20 Defining Functions 10

C: 'Start first’'Start second’'Caught second' 'Done second''No errors first' 'Done first'

D: 'Start first’ 'Start second’ 'No errors second''No errors first''Done first'

Page 11: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Which One is Closest to Your Answer?

A: 'Start first''Start second’AssertionError: '-1 is not <= 0'

B: 'Start first''Start second''1 is not <= 0’'Caught second' 'Done second''No errors first' 'Done first'

9/15/20 Defining Functions 11

C: 'Start first’'Start second’'Caught second' 'Done second''No errors first' 'Done first'

D: 'Start first’ 'Start second’ 'No errors second''No errors first''Done first'

E:

¯\_(ツ)_/¯

Page 12: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Which One is Closest to Your Answer?

A: 'Start first''Start second’AssertionError: '-1 is not <= 0'

B: 'Start first''Start second''1 is not <= 0’'Caught second' 'Done second''No errors first' 'Done first'

9/15/20 Defining Functions 12

C: 'Start first’'Start second’'Caught second' 'Done second''No errors first' 'Done first'

D: 'Start first’ 'Start second’ 'No errors second''No errors first''Done first'

CORRECT

Page 13: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Try-Except: Tracing Control Flow

def first(x):print('Start first.')try:

second(x)print('No errors first')

except:print('Caught first')

print('Done first')

def second(n):print('Start second.')try:

assert n <= 0, str(n)+' is not <= 0'print('No errors second')

except:print('Caught second')

assert n >= 0, str(n)+' is not >= 0'print('Ending second')

What is the output of first(-1)?

10/3/19 Asserts & Error Handling 13

Page 14: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Which One is Closest to Your Answer?

A: 'Start first''Start second''No errors second' 'Caught first' 'Done first'

B: 'Start first''Start second''No errors second' '-1 is not <= 0''Caught first' 'Done first'

9/15/20 Defining Functions 14

C: 'Start first''Start second''No errors second'AssertionError: '-1 is not <= 0'

D: 'Start first’ 'Start second’ 'No errors second''Done second''Caught first' 'Done first'

Page 15: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Which One is Closest to Your Answer?

A: 'Start first''Start second''No errors second' 'Caught first' 'Done first'

B: 'Start first''Start second''No errors second' '-1 is not <= 0''Caught first' 'Done first'

9/15/20 Defining Functions 15

C: 'Start first''Start second''No errors second'AssertionError: '-1 is not <= 0'

D: 'Start first’ 'Start second’ 'No errors second’'Done second''Caught first' 'Done first'

CORRECT

Page 16: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Try-Except: Tracing Control Flow

def first(x):print('Start first.')try:

second(x)print('No errors first')

except:print('Caught first')

print('Done first')

def second(n):print('Start second.')try:

assert n <= 0, str(n)+' is not <= 0'print('No errors second')

except:print('Caught second')

assert n >= 0, str(n)+' is not >= 0'print('Ending second')

What is the output of first(0)?

10/3/19 Asserts & Error Handling 16

Page 17: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Which One is Closest to Your Answer?

A: 'Start first''Start second''No errors second’ 'No errors first''Done first'

B: 'Start first''Start second''No errors second' ‘Done second''No errors first' 'Done first'

9/15/20 Defining Functions 17

C: 'Start first''Start second''Caught second’ 'No errors first' 'Done first'

D: 'Start first’ 'Start second’ 'No errors second''Caught first''Done first'

Page 18: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Which One is Closest to Your Answer?

A: 'Start first''Start second''No errors second’ 'No errors first''Done first'

B: 'Start first''Start second''No errors second' ‘Done second''No errors first' 'Done first'

9/15/20 Defining Functions 18

C: 'Start first''Start second''Caught second’ 'No errors first' 'Done first'

D: 'Start first’ 'Start second’ 'No errors second''Caught first''Done first'

CORRECT

Page 19: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Brainstorm: Without Conditionals

def isnetid(s):"""Returns: True if s is a potential netid.A potential netid is 2 or 3 lowercase letters followedby a sequence of digits

Precondition: nid is a string"""

10/3/19 Asserts & Error Handling 19

How can we implement this without if-statements?

Page 20: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

Brainstorm: Without Conditionals

def isnetid(s):"""Returns: True if s is a potential netid.A potential netid is 2 or 3 lowercase letters followedby a sequence of digits

Precondition: nid is a string"""

10/3/19 Asserts & Error Handling 20

Was this a good idea?

A: YesB: NoC: Unsure

Page 21: Asserts and Error Handling · §We will honor this –no class §Since no Wednesday lab, not Tuesday either •But will hold Zoom session Tuesday! •Today’s lab is due Thursday/Friday

9/17/20 Strings 21

Questions?


Recommended