如果然后Python声明麻烦 [英] Python if then statement trouble
问题描述
你好,
我正在编写一个无聊的python游戏,但是我有if / then语句的问题我将发布代码和错误。
错误
回溯(最近一次通话):
文件adv.py,第12行,在< module>
如果x == y:
NameError:name'y '未定义
代码:
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:
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>
推荐答案
y
是变量'y'
是一个值。
我想你要比较x
到值'y'
y
is a variable'y'
is a value.
I guess you want to comparex
to the value'y'
if x == 'y':
你的代码还有其他问题,你应该通过列出一个完整的模块或功能改进问题。
我不知道要知道你以前在你的代码项目中是否定义了Y,而是从包含的代码中定义,这里最明显的错误是:你要求
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
if X == Y
并且你没有预先指定
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'
然而,什么东西得到印刷?如果你得到误报,那将是非常不幸的....即你的y变量意外地等于x并且在零星的时间给出了预期的程序执行正回报。
或许定义之前的y会消除我们的任何歧义。
ie
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.
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'
我建议你工作通过 Python教程 [ ^ ]。
I suggest you work through The Python Tutorial[^].
这篇关于如果然后Python声明麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!