Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.4.2 | name错误:未定义名称“x”_Python_Python 3.x - Fatal编程技术网

Python 3.4.2 | name错误:未定义名称“x”

Python 3.4.2 | name错误:未定义名称“x”,python,python-3.x,Python,Python 3.x,我正在Python3.4.2中开发一个基于单词的迷宫游戏,当我尝试运行它时,遇到了这个错误消息:NameError:name'direction'未定义 我是这样定义它的: def chooseDirection(): direction = input('''What way will you go? (Type Left or Right or Forward then press enter.) ''') 然后我试着这样使用“方向”: if direction == str(Ri

我正在Python3.4.2中开发一个基于单词的迷宫游戏,当我尝试运行它时,遇到了这个错误消息:NameError:name'direction'未定义

我是这样定义它的:

def chooseDirection():
    direction = input('''What way will you go? (Type Left or Right or Forward then press enter.) ''')
然后我试着这样使用“方向”:

if direction == str(Right or right):
    print ('Congrats! You have turned the right way, you can live...')
    time.sleep(1)
    print ('For now o.O')
我找不到我的代码有任何问题,我已经从stackoverflow检查了一些其他类似的问题,但没有一个有效

如果您有任何想法,我们将不胜感激,如果您需要更多信息,请告诉我

谢谢,塞巴斯蒂安

def checkDirection(chooseDirection):

print('You have now entered the maze.')
time.sleep(0.5)

if direction == str(Right or right):
print ('Congrats! You have turned the right way, you can live...')
time.sleep(1)
print ('For now o.O')
将方向替换为ChoosedDirection,因为这是您尝试传递或获取值的参数

如果你用的话

if (direction == 'Right' or direction == 'Left'):


我想你想返回方向,而不是选择方向。

我检查了你的代码。你真的需要读一本关于Python的好教程

修正你的代码

首先,您应该将checkDirection参数更改为direction。checkDirection方法中没有方向变量

此外,您不处理chooseDirection方法的返回值

在while循环中,您应该将其作为一个变量,并使用它调用checkDirection


最后选择Direction不返回正确的变量。应该是方向

请在问题上粘贴完整代码。不要在代码中使用指向外部页面的链接。我认为您需要阅读一本关于Python的好教程。或者从当前使用的版本改成其他版本,因为显然这不好。非常感谢!我以为这会是一件非常明显的事情,但我仍然非常感激!
def chooseDirection():
direction = input('''What way will you go? (Type Left or Right or Forward then press enter.) ''')

return chooseDirection
def checkDirection(direction):
    ...
d = chooseDirection()
checkDirection(d)