- A run-time error happens when Python understands what you are saying, but runs into trouble when following your instructions.
- You need to know that Python runs the program in two steps.
- Not only Python features runtime errors and if you stumble across one, check our Runtime Errors Hub.
- We wrote extensively about developer tools like Python. Go to our Developer tools Hub.
Programming is pretty fun but at some point, when you try to run the program, unfortunately, and inevitably, errors will show up. So, causing, understanding, and fixing errors is an important part of programming.
There are three major types of error that you can get in Python but here we will talk about how to solve Python runtime errors.
A run-time error happens when Python understands what you are saying, but runs into trouble when following your instructions.
How can I fix Python runtime error?
So you’ve written everything correctly, in other words, your syntax is correct, but Python still doesn’t understand what you’re saying. Let’s have a simple example of a Python runtime error:
If you try to run this simple line, you will receive a runtime error simply because you didn’t define the solution variable. The line doesn’t have sense.
To understand better that conundrum, let’s make an association with English grammar. Let’s take this sentence for instance.
- Please eat the door.
Grammatically, there is absolutely no problem with this sentence. Everything is there, we have all the syntax elements correct.
But when you are trying to understand the sentence, when you piece up the words, it doesn’t make sense because you know you can open and close the door, even shut it or taking it out but eating it?
In programming, this will be called a run-time error because it shows up before you start the program. There are a few types of runtime errors. In this article, you will learn how to solve them.
1. Use an undefined variable or function.
This can also occur if you use capital letters inconsistently in a variable name:
callMe = “Brad”
Traceback (most recent call last): In line 2 of the code you submitted: print(callme) NameError: name 'callme' is not defined
In this case, the program returned the undefined variable error. You defined the variable callMe, but you try to print another variable, callme. You have to use the variables exactly as you define them, case sensitive.
2. Dividing by zero
Guess what? Python cares about math, and in math, dividing by zero makes no sense.
Traceback (most recent call last): In line 1 of the code you submitted: print(1/0) ZeroDivisionError: division by zero
So this line returns a runtime error as Python can read it properly, but when it comes to executing it, he refuses to do so as it has no mathematical sense.
3. Use operators on the wrong type of data
Traceback (most recent call last): In line 1 of the code you submitted: print("you are trying to add text and numbers" + 20) TypeError: can only concatenate str (not "int") to str
This line returns the runtime error because you try to add text with numbers, crayons, and oranges, chairs with pigeons, etc. It just doesn’t make sense to perform operations with different types of variables.
You also need to know that Python runs the program in two steps. It first checks the syntax and if the syntax is correct, it goes to the second step of executing the program. That’s where he stumbles on possible runtime errors.
We hope this guide answers all your questions but if you have any others or you run into other problems, please throw them down into the comments section below and we will get back at you ASAP.
Frequently Asked Questions
The fastest way to fix any runtime errors is to simply install a dedicated runtime error fixer.
If one of the lines contains a problem like undefined variables, dividing by zero or performing operations with variables of different types, it will return a runtime error.
The most simple example is a program with a simple line like print (1/0). If you run it, you will get a ZeroDivisionError: division by zero error, which is a runtime error.