Introduction
Displaying values
Variables
9. Undefined variables
The print function
List basics
Summary

Instruction

Good! Variables are created when you first assign a value to them. What happens when you try the following?

secret_number = 5
secret_number = random_number + secret_number

If random_number was never defined before, Python will execute the first line and then show an error when it reaches the second line. When you get an error, any code at or after the erroneous line will not be executed. In this case, the second line (and any other lines that come after it will not be executed).

Exercise

Run the template code.

We get the following result:

First, the current_age variable was assigned the value 12. Then, the following error appeared:

NameError: name 'unknown_age' is not defined

Note that everything after the unknown_age line was never executed because of the error:

current_age = current_age + 5
current_age