Python名称错误:名称'';未在类中定义

Python名称错误:名称'';未在类中定义,python,python-3.x,Python,Python 3.x,我正在尝试做一个很酷的小回合格斗游戏,我的代码中有一部分不喜欢我。我有一个菜单系统,不能决定一些事情。(请记住,我最近才开始上课。) 制作一个类实体(用于玩家和敌人) 制作一个玩家类(这样我可以添加更多的角色),里面有一个菜单 class Player(Entity): options = [menu(),attack(),items(),stats(),flee()] def menu(self): menuchoice = input('Menu\n

我正在尝试做一个很酷的小回合格斗游戏,我的代码中有一部分不喜欢我。我有一个菜单系统,不能决定一些事情。(请记住,我最近才开始上课。)

制作一个类实体(用于玩家和敌人)

制作一个玩家类(这样我可以添加更多的角色),里面有一个菜单

 class Player(Entity):
      options = [menu(),attack(),items(),stats(),flee()]
      def menu(self):
        menuchoice = input('Menu\n1. Attack\n2. Items\n3. Stats\n4. Flee\n')
        if menuchoice not in ['1','2','3','4']:
          clear()
          options[0]
        options[int(menuchoice)]
        options[0]
      def attack(self):
        print('attack')
      def items(self):
        print('items')
      def stats(self):
        print('stats')
      def flee(self):
        print('flee')
正在尝试运行菜单

player = Player()
player.menu()
错误

回溯(最近一次呼叫最后一次):
文件“main.py”,第16行,在
职业玩家(实体):
播放器中第17行的文件“main.py”
选项=[menu()、attack()、items()、stats()、runge()]
名称错误:未定义名称“菜单”
有人能告诉我如何在这个代码中定义菜单吗?我正在使用Python 3.6.1


编辑:谢谢!现在可以了。我必须添加
()
并将
选项=[…]
移动到末尾

变量options可能用于维护对可用选项的引用。它所做的是在定义方法之前调用方法本身:

下面是一个演示问题的最低版本(在python解释器本身上:

>>> class Player:
...     options = [menu()]
...     def menu(self):
...         print('menu')
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in Player
NameError: name 'menu' is not defined
>>>
>职业玩家:
…选项=[菜单()]
…def菜单(自我):
…打印('菜单')
...
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第2行,在播放器中
名称错误:未定义名称“菜单”
>>>
这是另一个版本,没有实际调用该方法,只是添加了引用。它会遇到相同的错误:

>>> class Player:
...     options = [menu]
...     def menu(self):
...         print('menu')
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in Player
NameError: name 'menu' is not defined
>>>
>职业玩家:
…选项=[菜单]
…def菜单(自我):
…打印('菜单')
...
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第2行,在播放器中
名称错误:未定义名称“菜单”
>>>
以下可能是您想要的:

>>> class Player:
...     options = ['menu']
...     def menu(self):
...         print('in menu')
...
>>> player = Player()
>>> player
<__main__.Player instance at 0x10a2f6ab8>
>>> player.options
['menu']
>>> player.options[0]
'menu'
>>> type(player.options[0])
<type 'str'>
>>> func = getattr(player, player.options[0])
>>> type(func)
<type 'instancemethod'>
>>> func
<bound method Player.menu of <__main__.Player instance at 0x10a2f6ab8>>
>>> func()
in menu
>>>
>职业玩家:
…选项=['菜单']
…def菜单(自我):
…打印('在菜单中')
...
>>>player=player()
>>>玩家
>>>玩家选项
[“菜单”]
>>>player.options[0]
“菜单”
>>>类型(播放器选项[0])
>>>func=getattr(player,player.options[0])
>>>类型(func)
>>>func
>>>func()
菜单中
>>>
如果在定义后使用它,则不会出现错误,但这不是标准/典型用法:

>>> class Player:
...     def menu(self):
...         print('in menu')
...     options = [menu]
...
>>> player = Player()
>>> player.options
[<function menu at 0x10a2f16e0>]
>>> player.options[0]
<function menu at 0x10a2f16e0>
>>> player.options[0]()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: menu() takes exactly 1 argument (0 given)
>>> player.options[0](player)
in menu
>>> Player.options[0](player)
in menu
>>> Player.options[0](player)   # calling with class reference
in menu
>>>
>职业玩家:
…def菜单(自我):
…打印('在菜单中')
…选项=[菜单]
...
>>>player=player()
>>>玩家选项
[]
>>>player.options[0]
>>>player.options[0]()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:menu()正好接受1个参数(给定0)
>>>播放器选项[0](播放器)
菜单中
>>>播放器选项[0](播放器)
菜单中
>>>Player.options[0](Player)#使用类引用调用
菜单中
>>>

在定义
选项=…
时,
菜单
尚未定义。将
选项=…
放在类的末尾。我假设您的意思是
选项=[菜单,攻击,…
,而不是实际调用方法
()
。但是,这些将是未绑定的,不太可能执行您想要的操作(提示:在
\uuu init\uuu()中定义
选项
获取绑定方法。我现在已经意识到了这一点,但我确实想调用这些方法,以便您能够与菜单交互。有没有其他方法可以使用与我不同的方法来实现这一点。我只是不想使用一堆if语句。您仍然可以调用它…例如:
选项[0]()
您仍然可以通过使用内置函数将函数转换为类方法来使用它。尝试使用
菜单
函数上方的
@classmethod
>>> class Player:
...     options = ['menu']
...     def menu(self):
...         print('in menu')
...
>>> player = Player()
>>> player
<__main__.Player instance at 0x10a2f6ab8>
>>> player.options
['menu']
>>> player.options[0]
'menu'
>>> type(player.options[0])
<type 'str'>
>>> func = getattr(player, player.options[0])
>>> type(func)
<type 'instancemethod'>
>>> func
<bound method Player.menu of <__main__.Player instance at 0x10a2f6ab8>>
>>> func()
in menu
>>>
>>> class Player:
...     def menu(self):
...         print('in menu')
...     options = [menu]
...
>>> player = Player()
>>> player.options
[<function menu at 0x10a2f16e0>]
>>> player.options[0]
<function menu at 0x10a2f16e0>
>>> player.options[0]()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: menu() takes exactly 1 argument (0 given)
>>> player.options[0](player)
in menu
>>> Player.options[0](player)
in menu
>>> Player.options[0](player)   # calling with class reference
in menu
>>>