Mimir is now part of HackerRank.
Learn More

Understanding Common Errors In Python

Picture of Prahasith Veluvolu

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.

python_syntax_breakdown

 

1. IndentationError

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.


2. SyntaxError: invalid syntax

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:


  • Forgetting parentheses around function calls and nested function calls.
  • Forgetting colons at the conditionals (if statements) and loops.
  • Trying to use a python protected word as a variable name.
  • You tried to create a variable that doesn’t start with a letter or underscore.
  • You used = instead of == when comparing that two variables are equal.

 


3. NameError

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.


4. IndexError: list index out of range

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.


5. TypeError: Can't convert 'int' object to str implicitly

 

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.


6. SyntaxError: EOL while scanning string literal

print('Hello World!) 

 

This error usually occurs if you forget a quote at the beginning or end of a string.


7. KeyError

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!

Create A Free Account