简体   繁体   English

为什么它一直显示 Python 3 代码错误类未定义但已定义?

[英]Why does it keep showing Python 3 Code Error Class Not Defined but it was defined?

I get the following error message:我收到以下错误消息:

Traceback (most recent call last):
  File "main.py", line 13, in <module>
    class Archer(User):
  File "main.py", line 22, in Archer
    archer1 = Archer('Joie', 100)
NameError: name 'Archer' is not defined

from this code:从这个代码:

class User():
      def sign_in(self):
        print('logged in')
    
    class Wizard(User):
      def __init__(self, name, power):
        self.name = name 
        self.power = power
    
      def attack(self):
        print(f'attacking with power of {self.power}')
    
    class Archer(User):
      def __init__(self, name, num_arrows):
        self.name = name 
        self.num_arrows = num_arrows
    
      def attack(self):
        print(f'attacking with arrows: arrows left- {self.num_arrows}')
    
      wizard1 = Wizard('John', 50)
      archer1 = Archer('Joie', 100)
      wizard1.attack()
      archer1.attack()

Why is the Archer class "not defined" at this point?为什么此时 Archer 类“未定义”?

Instead of indenting your last four lines you should have them like so:与其缩进最后四行,不如让它们像这样:

class Archer(User):
  def __init__(self, name, num_arrows):
    self.name = name 
    self.num_arrows = num_arrows

  def attack(self):
    print(f'attacking with arrows: arrows left- {self.num_arrows}')


wizard1 = Wizard('John', 50)
archer1 = Archer('Joie', 100)
wizard1.attack()
archer1.attack()

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

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