我目前正在研究一个简单的基于文本的Python游戏,只是为了练习python和面向对象的编程,但我遇到了这个错误,它告诉我'LargeManaPotion'没有属性'name',当我看到它确实如此,它的声明方式与'SmallManaPotion'完全相同.我假设这是一个愚蠢的错误,我只是在俯视或者什么但是会很感激帮助.此外,当我在player.inventory函数中打印播放器的库存时,程序将打印药水,所以我不确定为什么它在交易功能中不起作用.无论如何,这是相关的代码.提前致谢.
class ManaPotion:
def __init__(self):
raise NotImplementedError("Do not create raw ManaPotion objects.")
def __str__(self):
return "{} (+{} Mana)".format(self.name, self.mana_value)
class LargeManaPotion(ManaPotion):
def __init__(self):
self.name = "Large Mana Potion"
self.mana_value = 45
self.value = 40
class SmallManaPotion(ManaPotion):
def __init__(self):
self.name = "Small Mana Potion"
self.mana_value = 15
self.value = 10
Run Code Online (Sandbox Code Playgroud)
如您所见,它与SmallManaPotion相同.这是导致错误的函数.
class TraderTile(MapTile):
def intro_text(self):
return "A frail not-quite-human, not-quite-creature squats in the corner " \
"\nclinking his gold coins together. \nHe looks willing to trade."
def __init__(self, x, y):
self.trader …Run Code Online (Sandbox Code Playgroud)