'Name X is not defined even though it is
Here is the code:
from colorama import *
import os
os.system('color FF')
print(Fore.GREEN+' {:<20} '.format('['+ Fore.RED+'1'+Fore.GREEN+'] Πρωινά'),
Fore.GREEN+' {:^20} '.format('['+ Fore.RED+'2'+Fore.GREEN+'] Μεσημεριανά'),
Fore.GREEN+' {:>20} '.format('['+ Fore.RED+'3'+Fore.GREEN+'] Βραδινά'))
y = input()
# Πρωινά
if y == '1':
print("Ακόμα τίποτα")
x = input()
# Μεσιμεριανά
if y == '2':
print(Fore.GREEN + ' {:<20} '.format('['+ Fore.RED+'1'+Fore.GREEN+'] Μπριζόλες στον φούρνο'),
Fore.GREEN+' {:^20} '.format('['+ Fore.RED+'2'+Fore.GREEN+'] Πατάτες στον φούρνο'))
z = input()
# Βραδινά
if y == '3':
print("Ακόμα τίποτα")
c = input()
# Πρωινά
if x == '1':
f = open("mpriz_fourno.txt", "r")
file_contents = f.read()
print(file_contents)
f.close()
if x == '2':
f = open("asd.txt", "r", encoding="utf8")
file_contents = f.read()
print(file_contents)
f.close()
if x == '3':
f = open("patates.txt", "r")
file_contents = f.read()
print(file_contents)
f.close()
# Μεσιμεριανά
if z == '1':
f = open("patates_fourno.txt", "r", encoding="utf8")
file_contents = f.read()
print(file_contents)
f.close()
# Βραδινά
if c == '1':
print("test")
input(Fore.RED + "\nPress Enter to exit")
So basically, some parts of the code are in Greek so I guess you will not be able to understand everything. I am making a virtual food recipe book but when I try to run it I get the following error at some point:
line 29, in <module> if x == '1': NameError: name 'x' is not defined
What should I do?
Solution 1:[1]
The issue stems from x only being defined if either y == '1' or y == '2'.
There are a number of ways to fix this problem, depending on how the program should work.
For instance, you could initialize the variables to a default value.
x = ""
z = ""
c = ""
This way, they can be accessed without causing a crash, as they are defined in the namespace.
Alternatively, you might change the control flow of your program by chaining or nesting the if-statements. Ensuring that x is only accessed after initialization.
By using an else-clause, and giving x a value in each of the branches, x will always end up with a value. Regardless of which if-statement ends up being true.
y = input()
if y == '1':
x = input()
elif y == '2':
x = ""
...
elif y == '3':
x = ""
else:
x = ""
...
Structure with nesting if-statements:
if y == '1':
x = input()
if x == '1':
...
elif x == '2':
...
elif x == '3':
...
else:
print("X needs to be 1, 2 or 3")
Solution 2:[2]
In the question code, x was only defined inside the if y == '1': statement.
Unlike some other languages, x is actually undefined (ie. it does not even take the value None) until it becomes defined.
So when y is not equal to 1 the code containing x=input() is never executed. So x does not exist in the code as far as python is concerned since that line is never executed.
This is the actual corrected code with comments so that you can see the obvious error.
from colorama import *
import os
os.system('color FF')
print(Fore.GREEN+' {:<20} '.format('['+ Fore.RED+'1'+Fore.GREEN+'] ??????'),
Fore.GREEN+' {:^20} '.format('['+ Fore.RED+'2'+Fore.GREEN+'] ???????????'),
Fore.GREEN+' {:>20} '.format('['+ Fore.RED+'3'+Fore.GREEN+'] ???????'))
x = 100 # <---- add something here so that x is defined
y = input()
# ??????
if y == '1':
print("????? ??????")
x = input() # if y is not equal to 1 then x is never defined
# ???????????
if y == '2':
print(Fore.GREEN + ' {:<20} '.format('['+ Fore.RED+'1'+Fore.GREEN+'] ????????? ???? ??????'),
Fore.GREEN+' {:^20} '.format('['+ Fore.RED+'2'+Fore.GREEN+'] ??????? ???? ??????'))
z = input()
# ???????
if y == '3':
print("????? ??????")
c = input()
# ??????
if x == '1':
f = open("mpriz_fourno.txt", "r")
file_contents = f.read()
print(file_contents)
f.close()
if x == '2':
f = open("asd.txt", "r", encoding="utf8")
file_contents = f.read()
print(file_contents)
f.close()
if x == '3':
f = open("patates.txt", "r")
file_contents = f.read()
print(file_contents)
f.close()
# ???????????
if z == '1':
f = open("patates_fourno.txt", "r", encoding="utf8")
file_contents = f.read()
print(file_contents)
f.close()
# ???????
if c == '1':
print("test")
input(Fore.RED + "\nPress Enter to exit")
And here is a very simple snippet that highlights the error:
y = 99
# x is undefined
if y == 10:
x = 'hello world' # this line is never run since 99 != 10
# x is still not defined because the line above was never executed
# this print statement will fail because 'name 'x' is not defined'
print(x)
I hope that explanation is helpful along with the other useful comments.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Olav Aga |
| Solution 2 |
