def foo():
assert 1 == 2
def bar():
foo()
def baz():
bar()
baz()
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) Input In [4], in <cell line: 1>() ----> 1 baz() Input In [3], in baz() 1 def baz(): ----> 2 bar() Input In [2], in bar() 1 def bar(): ----> 2 foo() Input In [1], in foo() 1 def foo(): ----> 2 assert 1 == 2 AssertionError:
def err1(x):
return 8/x
err1(0)
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Input In [7], in <cell line: 1>() ----> 1 err1(0) Input In [6], in err1(x) 1 def err1(x): ----> 2 return 8/x ZeroDivisionError: division by zero
d = { "Jenny":8675309}
d['Mattox']
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Input In [9], in <cell line: 1>() ----> 1 d['Mattox'] KeyError: 'Mattox'
mylist = [2,3,4,5]
mylist[10]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Input In [11], in <cell line: 1>() ----> 1 mylist[10] IndexError: list index out of range
'Mattox' in d
False
mylist['hi']
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [13], in <cell line: 1>() ----> 1 mylist['hi'] TypeError: list indices must be integers or slices, not str
i = int(input("Give me an index: "))
mylist[i]
Give me an index: 999
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Input In [15], in <cell line: 2>() 1 i = int(input("Give me an index: ")) ----> 2 mylist[i] IndexError: list index out of range
def safeLookup():
try:
i = int(input("Give me an index: "))
return mylist[i]
except:
print("Ug. Something went wrong.")
safeLookup()
Give me an index: 10 Ug. Something went wrong.
safeLookup()
Give me an index: 3
5
def safeLookup():
try:
i = int(input("Give me an index: "))
return mylist[i]
except IndexError:
print("It needs to be from 0 to 3")
except ValueError:
print("Please stick with integers.")
except Exception as e:
print("Ug. Something went wrong.")
print(e)
safeLookup()
Give me an index: 234 It needs to be from 0 to 3
class BadDay(Exception):
pass
def howAreYou():
d = input("What day is it? ")
if d=='Monday':
raise BadDay
else:
print("Good day!")
howAreYou()
What day is it? Monday
--------------------------------------------------------------------------- BadDay Traceback (most recent call last) Input In [32], in <cell line: 1>() ----> 1 howAreYou() Input In [31], in howAreYou() 2 d = input("What day is it? ") 3 if d=='Monday': ----> 4 raise BadDay 5 else: 6 print("Good day!") BadDay: