我目前正在一個簡單的基於文本的遊戲在Python只是爲了實踐python和麪向對象的編程,但我遇到了這個錯誤告訴我'LargeManaPotion'沒有屬性'name',當我看到它的確如此,並且它被聲明爲與'SmallManaPotion'完全相同的工作方式。我假設這是一個愚蠢的錯誤,我只是俯視或什麼,但會感謝幫助。此外,當我在player.inventory函數中打印播放器的庫存時,程序將打印藥水,所以我不確定它爲什麼在交易函數中不起作用。無論如何,這裏是相關的代碼。提前致謝。Python的AttributeError:類型的對象'x'沒有屬性'x'
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
正如您所看到的,它與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 = npc.Trader()
super().__init__(x, y)
def trade(self, buyer, seller):
for i, item in enumerate(seller.inventory, 1):
#the line below here is where I'm getting the error.
print("{}. {} - {} Gold".format(i, item.name, item.value))
while True:
user_input = input("Choose an item or press Q to exit: ")
if user_input in ['q', 'Q']:
return
else:
try:
choice = int(user_input)
to_swap = seller.inventory[choice - 1]
self.swap(seller, buyer, to_swap)
except ValueError:
print("Invalid choice!")
def swap(self, seller, buyer, item):
if item.value > buyer.gold:
print("That's too expensive.")
return
seller.inventory.remove(item)
buyer.inventory.append(item)
seller.gold = seller.gold + item.value
buyer.gold = buyer.gold - item.value
print("Trade complete!")
def check_if_trade(self, player):
while True:
print("\n\nGold: {} \nWould you like to (B)uy, (S)ell, or (Q)uit?".format(player.gold))
user_input = input()
if user_input in ['Q', 'q']:
return
elif user_input in ['B', 'b']:
print("\n\nGold: {} \nHere's whats available to buy: ".format(player.gold))
self.trade(buyer=player, seller=self.trader)
elif user_input in ['S', 's']:
print("\n\nGold: {} \nHere's what's available to sell: ".format(player.gold))
self.trade(buyer=self.trader, seller=player)
else:
print("Invalid choice!")
但是,該函數調用LargeManaPotion,但沒有任何錯誤。
def print_inventory(self):
print("Inventory:")
for item in self.inventory:
print('* ' + str(item))
print("* Gold: {}".format(self.gold))
best_weapon = self.most_powerful_weapon()
print("Your best weapon is your {}".format(best_weapon))
錯誤和堆棧跟蹤:
Choose an action:
i: Print inventory
t: Trade
n: Go north
s: Go south
w: Go west
m: Replenish Mana
Action: t
Gold: 33
Would you like to (B)uy, (S)ell, or (Q)uit?
>>>b
Gold: 33
Here's whats available to buy:
1. Crusty Bread - 12 Gold
2. Crusty Bread - 12 Gold
3. Crusty Bread - 12 Gold
4. Healing Potion - 60 Gold
5. Healing Potion - 60 Gold
6. Small Mana Potion - 10 Gold
7. Small Mana Potion - 10 Gold
Traceback (most recent call last):
File "/Users/Cpt_Chirp/Documents/Escape/game.py", line 74, in <module>
play()
File "/Users/Cpt_Chirp/Documents/Escape/game.py", line 17, in play
choose_action(room, player)
File "/Users/Cpt_Chirp/Documents/Escape/game.py", line 30, in choose_action
action()
File "/Users/Cpt_Chirp/Documents/Escape/player.py", line 112, in trade
room.check_if_trade(self)
File "/Users/Cpt_Chirp/Documents/Escape/world.py", line 127, in check_if_trade
self.trade(buyer=player, seller=self.trader)
File "/Users/Cpt_Chirp/Documents/Escape/world.py", line 96, in trade
print("{}. {} - {} Gold".format(i, item.name, item.value))
AttributeError: type object 'LargeManaPotion' has no attribute 'name'
Process finished with exit code 1
請張貼錯誤消息和堆棧跟蹤。 – ozgur