[英]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.