随机导入
Class Item(): “”“所有项目的基类”“” def init (自身,名称,描述,值): self.name =名称 self.description =说明 self.value =值 self.quality =质量
def __str__(self):
return "{}\n=====\n{}\nValue: {}\n".format(self.name, self.description, self.value)
金级(物品): def init (自我,amt): self.amt = amt super()。 init (name =“ Gold”, description =“圆形硬币,正面印有{}。”。format(str(self.amt)), value = self.amt)
武器类(物品):
Conditions = { 1 : "Bad Condition", 2 : "Bad Condition",
3 : "Good Condition", 4 : "Good Condition",
5 : "Perfect Condition" }
def __init__(self, name, description, value, damageRange):
self.name = name
self.description = description
self.value = value
self.condition = Weapon.Conditions[self.damage]
self.qualtiy = damage
类岩石(武器): def init (自己): super()。初始化(“摇滚”, “拳头大小的岩石,适合打锤。”, 0, 范围(1,5))
匕首类(武器): def init (自己): super()。初始化(“匕首”, “带有锈迹的小匕首。比岩石还危险。”, 10 范围(5,11))
长剑类(武器): def init (自己): super()。初始化(“长剑”, “由传奇的铁匠艾尔雷德·吉尔里克(Ailred Gilric)锻造的剑”, 15 范围(15,20))
错误-AttributeError:“长剑”对象没有属性“损坏”
答案 0 :(得分:2)
您不能引用您在线提供的参数:
super().__init__(name="Rock", description="A fist-sized rock, suitable for bludgeoning.", value=0, damage=random.randint(1,5), 1 = "Bad Condition", # does not work, you could put them into a # dict and provide that. 2 = "Bad Condition", 3 = "Good Condition", 4 = "Good Condition", 5 = "Perfect Condition", quality = damage) # can not reference damage here, damage is # the keyword the exception complains about
您可以这样解决此问题:
import random
class Weapon:
# this is a shared class variable for all weapons and maps a damage-value
# to a textual condition
Conditions = { 1 : "Bad Condition", 2 : "Bad Condition",
3 : "Good Condition", 4 : "Good Condition",
5 : "Perfect Condition" }
def __init__(self,name,desc,value,dmgRange):
self.name = name
self.description = desc
self.value = value
# switched to random.choice() over the given range
self.damage = random.choice(dmgRange)
self.condition = Weapon.Conditions[self.damage]
self.quality = self.damage
class Rock(Weapon):
# condition and quality are dependent on the damage, moved them into the
# base classes __init__
# moved the random damage also into the base class, so it decides on the damage
# you would have to replicate that make a random damage in each weapon...
def __init__(self):
# range(1,6) : 6 is exclusive, so you get 1,2,3,4,5
super().__init__("Rock","A fist-sized rock, suitable for bludgeoning.",
0,range(1,6))
def __str__(self):
return f"A {self.name} in {self.condition}. Its quality rates {self.quality} " \
+ f"and it deals {self.damage} damage. It cost: {self.value} coins."
print("You inspect the beach and find:")
for _ in range(10):
print("-",Rock())
输出:
You inspect the beach and find:
- A Rock in Perfect Condition. Its quality rates 5 and it deals 5 damage. It cost: 0 coins.
- A Rock in Bad Condition. Its quality rates 1 and it deals 1 damage. It cost: 0 coins.
- A Rock in Bad Condition. Its quality rates 2 and it deals 2 damage. It cost: 0 coins.
- A Rock in Good Condition. Its quality rates 4 and it deals 4 damage. It cost: 0 coins.
- A Rock in Good Condition. Its quality rates 4 and it deals 4 damage. It cost: 0 coins.
- A Rock in Bad Condition. Its quality rates 1 and it deals 1 damage. It cost: 0 coins.
- A Rock in Good Condition. Its quality rates 4 and it deals 4 damage. It cost: 0 coins.
- A Rock in Bad Condition. Its quality rates 2 and it deals 2 damage. It cost: 0 coins.
- A Rock in Bad Condition. Its quality rates 2 and it deals 2 damage. It cost: 0 coins.
- A Rock in Good Condition. Its quality rates 4 and it deals 4 damage. It cost: 0 coins.
阅读:
dict
ionaries random.choice()
range(start,stop[,step]
) __str__()
-同时查看__repr__()
,您可能也想更改那个