[英]Why does it keep showing Python 3 Code Error Class Not Defined but it was defined?
我收到以下错误消息:
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
从这个代码:
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()
为什么此时 Archer 类“未定义”?
与其缩进最后四行,不如让它们像这样:
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.