Prahasith Veluvolu
March 21, 2019
When we were building our automated common error feedback feature on Mimir Classroom, we analyzed millions of stack traces that our students received when completing their coursework on our platform. In this post, you will find the errors we found to be most troubling to students in Python and some tips on how to approach them.
To start off, here is a quick refresher on how to read an error in the Python stack trace.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
This error means that you have inconsistent indenting in your code. Check the line number reported by this error and double check that your indents around compound statements (conditionals and loops) and functions are consistent. If you have trouble solving this, ensure that all of your indents are the same. For example, mixing whitespace based indents and tabs based indents can cause this error.
def factorial(n):
if n == 0
return 1
else
return n * factorial(n - 1)
print(factorial(5))
This is a very broad error but we see it most often caused by the following:
input_name = input('What is your name?\n')
print('Hi, %s.' % name)
This error occurs when Python encounters a variable that it does not recognize. Make sure that you initialize your variables before you use them and check for typos in your variables names. Remember that Python is case sensitive.
my_list = [1, 2, 3]
print(my_list[3])
This error occurs when you try to access a spot in a list that does not exist. For example, if you have a list with 3 items called sampleList, then you can access the three in the list with the following: sampleList[0] // First item sampleList[1] // Second item sampleList[2] // Third item Indexes in python always start at 0 and end at n-1 where n is the size of the list.
This error happens when you try to concatenate an int into a string. Try surrounding your number variable with the str( ) function to convert it into a string.
print('Hello World!)
This error usually occurs if you forget a quote at the beginning or end of a string.
foo = {'one': 'A', 'two': 'B', 'three': 'C'}
print(foo['four'])
This error occurs when you try to access a key in a dictionary that does not exisit.
If you have any other errors that you find your students encountering often, reach out to us at hello@mimirhq.com and let us know!