Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hello,
I'm writing a python game to play when bored but i'm having issuses with an if/then statement I will post code and error.

Error

Traceback (most recent call last):


File "adv.py", line 12, in <module>
if x == y:
NameError: name 'y' is not defined

Code:

Python
    x = raw_input('[Rome] ---> [y/n]')
if x == y: 

	print '[Nation] Rome has taken over Gaul'
	print '[Nation] Gaul has fallen into ruins.'
	
 

    else:
    	print 'Gaul has defeated Rome'</module>
Posted
Updated 17-Jan-17 18:12pm
v2
Comments
Member 12120337 6-Nov-15 15:07pm    
The module didn't work

y is a variable 'y' is a value.
I guess you want to compare x to the value 'y'
Python
if x == 'y': 


Your code have other problems, you should improve question by listing a complete module or function.
 
Share this answer
 
Comments
ZurdoDev 6-Nov-15 15:11pm    
+5
Patrice T 6-Nov-15 15:32pm    
Thank you
I don't know if you define Y previously in your code project but from the included code, the most glaring error here is: you are asking
Python
if X == Y
and you aren't specifying
XML
Y
before hand.

So basically; if variable equal to undefined variable, execute below code:

"
print('[Nation] Rome has taken over Gaul')
print ('[Nation] Gaul has fallen into ruins.'):
else:
    print 'Gaul has defeated Rome'

However, how often is anything getting printed? It would be pretty unfortunate if you were getting false positives.... i.e. your y variable accidentally equals x and gives the intended positive return of program execution at sporadic times.


Perhaps defining y before hand would take away any ambiguity for us.

i.e.
Python
y = f[0]
x = raw_input('[Rome] ---> [y/n]')
if x == y: 
 
	print '[Nation] Rome has taken over Gaul'
	print '[Nation] Gaul has fallen into ruins.'
	
 
 
    else:
    	print 'Gaul has defeated Rome'
 
Share this answer
 
Comments
Patrice T 9-Nov-15 16:12pm    
y is unlikely to be a variable. if you look at before test, x is loaded with the yes/no answer to a question, it probable that one want to check what is the answer.
PythonHeadBanger 9-Nov-15 16:14pm    
This is true. But I felt as though "y is a variable 'y' is a value" didn't go far enough to explain that he has referred to a variable y in his code, when he intends to test if x == 'y'. Thus, a literal interpretation of the code that he wrote, and should help him identify his error.
I suggest you work through The Python Tutorial[^].
 
Share this answer
 
In this line:
if x == y:

You have not said y is a string but instead python thinks it is a variable. simply change that line to the following:
if x == "y" or x == "Y":

(Please note I added the case where Y is a capital letter)
Remember, strings have the quotes (") around them and variables do not! :)
 
Share this answer
 
v2
Comments
Richard MacCutchan 18-Jan-17 5:00am    
You are more than a year too late.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900