You assigned the number 5
to the variable x
. But then you told Python to print y
— and nowhere in your code has y
ever been given a value. Python’s error message:
NameError: name 'y' is not defined
is its way of saying “I’ve never heard of y
.”
To fix it, you need to make sure you print the same variable you defined:
x = 5
print(x) # prints 5
Think of variables like labels on boxes. You wrote the number 5
on a sticky note labeled “x” and stuck it on a box. Then you asked Python, “What’s in the box called y
?” But there is no box labeled y
, so it complained. The cure is simply: use the same label consistently.
If you want both x
and y
to exist, you’d have to assign both:
x = 5
y = 5
print(y) # also prints 5