Practice: Debugging#

Semantic Errors#

Semantic errors come about when your program does not work as it is supposed to. These usually do not lead to an actual error message, the program instead outputs something you did not expect or want, or does not output anything when you were expecting it to.

Example 1#

Intent: program that takes two numbers by user input, adds them together, and prints the sum

Reproducible example#

num1 = input('Enter a number:') 
num2 = input('Enter another number:') 
result = num1 + num2 
print('The sum of', num1, 'and', num2, 'is', result)
---------------------------------------------------------------------------
StdinNotImplementedError                  Traceback (most recent call last)
/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_89653/2485259710.py in <module>
----> 1 num1 = input('Enter a number:')
      2 num2 = input('Enter another number:')
      3 result = num1 + num2
      4 print('The sum of', num1, 'and', num2, 'is', result)

~/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
   1001         """
   1002         if not self._allow_stdin:
-> 1003             raise StdinNotImplementedError(
   1004                 "raw_input was called, but this frontend does not support input requests."
   1005             )

StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.

Debugging notes#

# debugging notes here!

# what did you expect?

# what did you see instead?

# what do you think is going on? what's the mismatch between your model and the computer's?
# checklist of strategies to draw from:
# [ ] model
# [ ] document/comment the code
# [ ] test with diff. inputs
# [ ] add print statements to help model what is happening / use python tutor

Fixed version:#

Explanation of fix:#

Example 2#

Intent: program that subtracts 2 from a given number until it reaches 0

Reproducible example#

num = 30 
while num >= 0: 
    num = num - 2 
    print(f'num is now {num}') 
num is now 28
num is now 26
num is now 24
num is now 22
num is now 20
num is now 18
num is now 16
num is now 14
num is now 12
num is now 10
num is now 8
num is now 6
num is now 4
num is now 2
num is now 0
num is now -2

Debugging notes#

# debugging notes here!

# what did you expect?

# what did you see instead?

# what do you think is going on? what's the mismatch between your model and the computer's?
# checklist of strategies to draw from:
# [ ] model
# [ ] document/comment the code
# [ ] test with diff. inputs
# [ ] add print statements to help model what is happening / use python tutor

Fixed version:#

Explanation of fix:#

Example 3#

Intent: program that prints all even numbers from 1 to 10

Reproducible example#

nums = [1,2,3,4,5,6,7,8,9,10]
for num in nums: 
    if num%2 != 0: 
        print(nums) 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Debugging notes#

# debugging notes here!

# what did you expect?

# what did you see instead?

# what do you think is going on? what's the mismatch between your model and the computer's?
# checklist of strategies to draw from:
# [ ] model
# [ ] document/comment the code
# [ ] test with diff. inputs
# [ ] add print statements to help model what is happening / use python tutor

Fixed version:#

Explanation of fix:#

Example 4#

Intent: program that takes two numbers and prints out if the first number is greater than or less than the second number

Reproducible example#

a = 3
b = 3

if (a > b):
    print("a is greater than b")
else:
    print("a is less than b")
a is less than b

Debugging notes#

# debugging notes here!

# what did you expect?

# what did you see instead?

# what do you think is going on? what's the mismatch between your model and the computer's?
# checklist of strategies to draw from:
# [ ] model
# [ ] document/comment the code
# [ ] test with diff. inputs
# [ ] add print statements to help model what is happening / use python tutor

Fixed version:#

Explanation of fix:#

Example 5#

Intent: program that adds course codes (strings) from one list to a new list if the courses are from INST (contain code INST)

Reproducible example#

items = ["INST201", "CMSC100", "STAT100", "INST126", "INST326"]
output = []
keyphrase = "INST"
for item in items:
    if "INST" in items:
        output.append(item)
print(output)
[]

Debugging notes#

# debugging notes here!

# what did you expect?

# what did you see instead?

# what do you think is going on? what's the mismatch between your model and the computer's?
# checklist of strategies to draw from:
# [ ] model
# [ ] document/comment the code
# [ ] test with diff. inputs
# [ ] add print statements to help model what is happening / use python tutor

Fixed version:#

Explanation of fix:#

Syntax Errors#

Syntax errors come from the user failing to follow the set rules given by python (e.g., syntax errors, indentation errors, index errors. These almost always lead to some type of error message

Example 1#

Intent: Program that checks if a number is even or odd

Reproducible example#

x = input('Enter a number (or "exit" to quit): ') # get initial input from user

while x != "exit": # check if we should keep going (stop if user says "exit")
    if int(x)%2 = 0: # check if number is even
        print('You have entered an even number.')
    else:
        print('You have entered an odd number.')
    x = input('Enter a number (or "exit" to quit): ') # get new input from user
  File "/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_7316/1250403228.py", line 4
    if int(x)%2 = 0: # check if number is even
                ^
SyntaxError: invalid syntax

Debugging notes#

# debugging notes here!

# what did you expect?

# what did you see instead?

# what do you think is going on? what's the mismatch between your model and the computer's?
# checklist of strategies to draw from:
# [ ] model
# [ ] document/comment the code
# [ ] test with diff. inputs
# [ ] add print statements to help model what is happening / use python tutor

Fixed version:#

Explanation of fix:#

Example 2#

Intent: program that checks if a list of numbers are even or odd

Reproducible example#

numbers = [3, 14, 58, 9, 88, 104, 25]

for num in numbers:
if num % 2 == 0:
    print("Even")
else:
    print("Odd")
  File "/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_7316/2619565430.py", line 4
    if num % 2 == 0:
    ^
IndentationError: expected an indented block

Debugging notes#

# debugging notes here!

# what did you expect?

# what did you see instead?

# what do you think is going on? what's the mismatch between your model and the computer's?
# checklist of strategies to draw from:
# [ ] model
# [ ] document/comment the code
# [ ] test with diff. inputs
# [ ] add print statements to help model what is happening / use python tutor

Fixed version:#

Explanation of fix:#

Example 3#

Intent: program to see how “small” a number is (Under 5 is smallest, under 25 is smaller, under 50 is smallest, other is not small)

Reproducible example#

num = 4
if num < 5
    print (str(num) + " is smallest")
elif num < 25
    print(str(num) + " is smaller")
elif num < 50
    print (str(num) " is small")
else
    print (str(num) + " is not small")
  File "/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_7316/3819850021.py", line 2
    if num < 5
              ^
SyntaxError: invalid syntax

Debugging notes#

# debugging notes here!

# what did you expect?

# what did you see instead?

# what do you think is going on? what's the mismatch between your model and the computer's?
# checklist of strategies to draw from:
# [ ] model
# [ ] document/comment the code
# [ ] test with diff. inputs
# [ ] add print statements to help model what is happening / use python tutor

Fixed version:#

Explanation of fix:#

Example 4#

Intent: program that compares two numbers. Check if the first number is divisible by the second (no remainder)

Reproducible example#

first_num = 10
second_num = 2

if frist_num % second_num == 0:
    print(first_num , "is divisible by", second_num)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_7316/1176868596.py in <module>
      2 second_num = 2
      3 
----> 4 if frist_num % second_num == 0:
      5     print(first_num , "is divisible by", second_num)

NameError: name 'frist_num' is not defined

Debugging notes#

# debugging notes here!

# what did you expect?

# what did you see instead?

# what do you think is going on? what's the mismatch between your model and the computer's?
# checklist of strategies to draw from:
# [ ] model
# [ ] document/comment the code
# [ ] test with diff. inputs
# [ ] add print statements to help model what is happening / use python tutor

Fixed version:#

Explanation of fix:#

Example 5#

Intent: program that prints all the items in a list

Reproducible example#

mylist = ["Garlic", "Lime", "Onion", "Banana", "Broccoli", "Pomegranate"]

for item in mylist:
    print(item
  File "/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_7316/2681184233.py", line 4
    print(item
              ^
SyntaxError: unexpected EOF while parsing

Debugging notes#

# debugging notes here!

# what did you expect?

# what did you see instead?

# what do you think is going on? what's the mismatch between your model and the computer's?
# checklist of strategies to draw from:
# [ ] model
# [ ] document/comment the code
# [ ] test with diff. inputs
# [ ] add print statements to help model what is happening / use python tutor

Fixed version:#

Explanation of fix:#