【发布时间】:2020-04-24 22:43:09
【问题描述】:
我正在学习课程。我创建了一个 Item 类,它定义了一个项目的统计信息,例如一把剑。我还创建了一个带有物品清单的 Player 类。我在 Item 类中添加了一个 super_sword,现在我想将 super_sword 添加到 Player 库存中。我收到以下错误:
line 16, in add_item
item.append(inventory)
AttributeError: 'Item' object has no attribute 'append'
我的代码:
class Item:
def item_stats():
base_dmg = 0
magic_dmg = 0
#--- Create an item super_sword and assign damage attributes ---#
super_sword = Item()
super_sword.base_dmg = 10
super_sword.magic_dmg = 12
class Player:
inventory = []
def add_item(self, item):
item.append(inventory)
#--- Create some_character and add super_sword to its inventory ---#
some_character = Player()
some_character.add_item(super_sword)
【问题讨论】:
-
请在
append返回您的教学资料以了解用法。
标签: python python-3.x class