x = 27
y = 33
my_list = [1, 17, 99, 0, '42']
string_1 = 'Hello'
string_2 = 'World'Expert Debugger
Understanding errors, tracebacks, and debugging is essential to quality programming. Remember not all bugs raise errors! Be on the lookout for code that runs, but does the wrong thing.
This interactive notebook uses only pure Python, no external resources to get started.
A number of bugs are presented in this notebook…can you solve them all?
Initial variables
#print x
print(y)33
total = 0
for i in my_list:
total += i
print(total)1
18
117
117
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[3], line 3 1 total = 0 2 for i in my_list: ----> 3 total += i 4 print(total) TypeError: unsupported operand type(s) for +=: 'int' and 'str'
# print item with value 99 from my_list
print(my_list[99])--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[4], line 2 1 # print item with value 99 from my_list ----> 2 print(my_list[99]) IndexError: list index out of range
#print all items in my_list
for i in my_list:
print iCell In[5], line 3 print i ^ IndentationError: expected an indented block after 'for' statement on line 2
# Print "Hello World" properly formatted on a single line
print(string_1)
print(string_2)Hello
World
# Print sum intial x, y
x = 'bug'
print(x+y)--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[7], line 3 1 # Print sum intial x, y 2 x = 'bug' ----> 3 print(x+y) TypeError: can only concatenate str (not "int") to str
# Enumerate the my_list
# Bonus, if count starts at 1
for i in range(10):
print(f'Count {i}: Value {my_list[i]}')Count 0: Value 1
Count 1: Value 17
Count 2: Value 99
Count 3: Value 0
Count 4: Value 42
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[12], line 4 1 # Enumerate the my_list 2 # Bonus, if count starts at 1 3 for i in range(10): ----> 4 print(f'Count {i}: Value {my_list[i]}') IndexError: list index out of range
Uncomment and get print to run on all the below
# print(x# print(33')# 0bugz = 0
# print(0bugz)# My Var = 'My Special Var'
# print(My Var)# Print('this')# bad_bug == 'Dung Beetle"
# print(bad_bug)