简体   繁体   English

Python NameError:名称“...”未在类中定义

[英]Python NameError: name '...' is not defined in a class

I'm trying to make a cool little turn based fighting game and one part of my code doesn't like me.我正在尝试制作一个很酷的回合制格斗游戏,但我的代码的一部分不喜欢我。 I have a menu system that can't determine a few things.我有一个无法确定一些事情的菜单系统。 (Please keep in mind, I just started classes very recently.) (请记住,我最近才开始上课。)

Making a class entity (for players and enemies).制作一个类实体(对于玩家和敌人)。

class Entity:
  def __init__(self):
    self.name = ''
    self.health = 0
    self.moveset = []

Making a player class (so I can add more characters) with a menu inside.制作一个带有菜单的播放器类(以便我可以添加更多角色)。

 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')

Trying to run the menu试图运行菜单

player = Player()
player.menu()

Error错误

Traceback (most recent call last):
  File "main.py", line 16, in <module>
    class Player(Entity):
  File "main.py", line 17, in Player
    options = [menu(),attack(),items(),stats(),flee()]
NameError: name 'menu' is not defined

Can someone tell me how to define the menu in this code?有人能告诉我如何在这段代码中定义菜单吗? I'm using Python 3.6.1.我正在使用 Python 3.6.1。

Edit: Thanks!编辑:谢谢! It works now.它现在有效。 I had to add () and move options = [...] to the end!我必须添加()并将options = [...]移到最后!

The variable options is likely intended to maintain a reference to the available options.变量options可能旨在维护对可用选项的引用。 What it's doing instead is calling the method itself before even the method is defined:它正在做的是在定义方法之前调用方法本身:

Here's a minimal version that demonstrates the problem (it's on the python interpreter itself:这是一个演示问题的最小版本(它在 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
>>>

Here's another version without actually calling the method but just adding the reference.这是另一个版本,没有实际调用方法,只是添加了引用。 It'll hit the same error:它会遇到同样的错误:

>>> 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
>>>

Here's probably what you want:这可能是你想要的:

>>> 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
>>>

Here's the proof if you use it after defining, it won't hit the error - but this isn't the standard/typical usage:这是在定义后使用它的证明,它不会出错 - 但这不是标准/典型用法:

>>> 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
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM