Python 名称错误:名称'';未定义-在尝试获取输入后看到
我一直在尝试做一个类似于测试的游戏来了解课程以及它们是如何工作的。我的计划是:Python 名称错误:名称'';未定义-在尝试获取输入后看到,python,class,nameerror,Python,Class,Nameerror,我一直在尝试做一个类似于测试的游戏来了解课程以及它们是如何工作的。我的计划是: print('The Dark Tunnel 2: Electric Boogaloo') win = False class Room(object): def __init__(self, location): self.location = location def move(self): self.move = move pr
print('The Dark Tunnel 2: Electric Boogaloo')
win = False
class Room(object):
def __init__(self, location):
self.location = location
def move(self):
self.move = move
print(rooms[self.move])
def look(self):
self.look = look
print(secrets[self.look])
rooms = {'north': 'You are in a dark room.', 'south': 'You are in a bloody room.', 'west': 'You are in a wet room.', 'east': 'You are in a grungy room.'}
secrets = {'north': 'There doesn\'t seem to be anything you missed.', 'south': 'There is an old coin by your feet.', 'west': 'There is too much water at your feet.', 'east': 'Is there someone behind those bars?'}
while win == False:
action = input('> ')
if action in rooms:
Room.move(action)
else:
Room.look(action)
输入“north”后,我想会显示“你在一个黑暗的房间里”,我得到:
Traceback (most recent call last):
File "program.py", line 20, in <module>
Room.move(action)
File "program.py", line 9, in move
self.move = move
NameError: name 'move' is not defined
回溯(最近一次呼叫最后一次):
文件“program.py”,第20行,在
房间。移动(动作)
文件“program.py”,第9行,移动中
self.move=移动
名称错误:未定义名称“移动”
如果希望move
方法接受名为move
的参数,则需要在定义方法时指定该参数:
def move(self, move):
self.move = move
除了属性move
需要使用与方法move
不同的名称之外;否则,您将使用提供的值替换方法self.move
def set_move(self, move):
self.move = move
然后您需要使用Room
的实例来调用它,而不是类本身
r = Room('some location')
...
r.set_move(action)
问题正是错误所说的:您没有定义
move
,但却调用了它。不确定您在move
函数中做了什么。在move函数中,我试图让它输出您在“action”中输入的值,谢谢。你是一个真正的兄弟