Runtimeerror python

As you do more and more programming, you will naturally encounter a lot of errors (or bugs). Causing, understanding, and fixing errors is an important part of programming. Python will do its best to run anything that you tell it to run, but if it can't understand what you're asking, then it won't run the program. All the same, Python will try to tell you a little bit of information about what went wrong, in order to help you try to fix it.

Here are two Python errors.

Example: A Syntax Error

In this first example, we forget to use the parenthesis that are required by print(...). Python does not understand what you are trying to do.

Here is a second example of a bug in Python.

Example: A Run-Time Error

In the second example, we forget to define the greeting variable. Python knows what you want it to do, but since no greeting has been defined, an error occurs.

A syntax error happens when Python can't understand what you are saying. A run-time error happens when Python understands what you are saying, but runs into trouble when following your instructions.

In English, a syntax error would be like the sentence

Please cat dog monkey.

The grammar of this sentence does not make sense. From the perspective of English grammar, it is missing a verb (action). You cannot understand what you are being asked to do. Syntax means the same thing as grammar.

In English, a run-time error would be like the sentence

Please eat the piano.

The sentence makes sense from a grammatical perspective — there is a verb and noun in the right place — so you know what you are being asked to do. But, you will encounter problems once you start trying to eat the piano (except maybe if you are a termite). This is called a run-time error because it occurs after the program starts running.

We also will talk about logic errors at the end, which means that your program runs without crashing, but still produces the wrong result. An example would be

Please close the back door so that the bugs don't come in.

This would be a logic error if the front door is also open: even though the command makes sense and you can close the back door, this doesn't actually accomplish the goal of keeping the bugs out since they'll still come in the front door.

Common Syntax Errors in Python

Here are a few additional examples of syntax errors that can occur in Python. One very general thing that can occur is that Python will encounter a special symbol in a place that it doesn't expect.

Example: Syntax Error

Python says SyntaxError: invalid syntax and points with ^ to the exclamation point. The problem is that ! has no meaning in Python. The syntax error would go away if we had put print("Hello, World!") instead, because then Python would understand that the ! is part of the text with Hello, World.

Here is another syntax error that is more subtle.

Example: Syntax Error

The problem is that class is a special word in Python. if you had written greeting0 instead of class it would have been fine. Click here to see the list of all special "keywords" in Python.

If you are using quotes around text and you forget the second one, or you are using parentheses and forget the second one, you will get syntax errors:

Example: Syntax Error

Forgetting the second quote

In this error, EOL is short for End Of Line: Python expected another greeting1 but the line ended before it was found.

Example: Syntax Error

Forgetting the second parenthesis

Similarly, EOF is short for End Of File: Python kept looking for a greeting2 but the program file ended before it was found.

Sometimes two very similar syntax errors can give two very different error messages. But, every error message is indeed trying to tell you something helpful.

Run-Time Errors

Here are a few common run-time errors. Python is able to understand what the program says, but runs into problems when actually performing the instructions.

  • using an undefined variable or function. This can also occur if you use capital letters inconsistently in a variable name:

    Example

    An undefined variable

  • dividing by zero, which makes no sense in mathematics. (Why? Since 0 times any number is 0, there is no solution to 1 = X * 0, so 1/0 is undefined.)

    Example

    Dividing by zero

  • using operators on the wrong type of data

    Example

    Adding text and a number

You will find more ways of producing errors as you learn more about Python.

What is the technical difference between syntax and run-time errors? Here is an example comparing a run-time error to a syntax error. Look at the output of each program.

Example: Run-Time Error

Example: Syntax Error

The program with the run-time error created some output, but the one with the syntax error did not. This is because Python runs in two steps:
  1. Python checks if your program has correct syntax, in order to determine its structure and parts.
  2. If no syntax errors were encountered in step 1, then the program is executed.

So, a program with a syntax error will execute no steps at all, but a program with a run-time error will execute the steps that happened before the error occured.

Logic Errors

Your program might run without crashing (no syntax or run-time errors), but still do the wrong thing. For example, perhaps you want a program to calculate the average of two numbers: the average of x and y is defined as

Why doesn't this program work?

Example

This does not calculate the average correctly.

The average should be

but the program prints greeting3 instead! The error this time has to do with the "order of operations" in arithmetic. When you write greeting4, this has the same mathematical meaning as . To fix the problem, the third line of our program should be written as greeting5, which makes clear to Python that we really want the value , where we add first and divide afterwards.

You can have logic errors because you designed a program incorrectly, or because you didn't write code that follows the design correctly (like the greeting6 example). Logic errors can be difficult to spot, especially in a longer program, but as you get better at writing code you will also get better at avoiding logic errors. Lesson 6D will give some tips on avoiding logic errors.

Exercises

Now that the lesson is complete, we have three exercises on debugging (fixing errors in programs). You can try to spot the errors before running the programs, or you can run them first and use Python's response to determine what needs fixing.

What is RuntimeError in Python?

A runtime error is a type of error that occurs during program execution. The Python interpreter executes a script if it is syntactically correct. However, if it encounters an issue at runtime, which is not detected when the script is parsed, script execution may halt unexpectedly.

How do I get rid of runtime error in Python?

How can I fix Python runtime error?.
Use an undefined variable or function. This can also occur if you use capital letters inconsistently in a variable name: callMe = “Brad” ... .
Dividing by zero. Guess what? ... .
Use operators on the wrong type of data. print(“you are trying to add text and numbers” + 20).

When to use RuntimeError?

A RuntimeError is raised when attempting to perform an invalid operation of some kind, which doesn't meet the criteria of a more specific error classification.

What is an example of a runtime error?

A program crash is the most noticeable type of runtime error since the program unexpectedly quits while running. Crashes can be caused by memory leaks or other programming errors. Common examples include dividing by zero, referencing missing files, calling invalid functions, or not handling certain input correctly.