gpt4 book ai didi

python - 将不同的变量分配给不同的类

转载 作者:行者123 更新时间:2023-11-30 23:23:11 24 4
gpt4 key购买 nike

我正在学习 Python 教程系列,并且我已经来上课了。所以..我正在尝试制作某种“中世纪RPG等级系统”,并尝试为战士等级获取武器。我对此真的很陌生,所以如果你们能尽可能简单地解释它,我将不胜感激。

所以,我收到一个错误:

AttributeError: 'Warrior' object has no attribute 'wep_name'

我做错了什么?

代码如下:

class Character(object):
def __init__(self, name):
self.health = 100
self.name = name
self.equipment = {

"Weapon": 'None',
"Attack Damage": '0'
}

def printName(self):
print "Name: " + self.name


class Warrior(Character):
"""
**Warrior Class**
50% more HP
"""
def __init__(self, name):
super(Warrior, self).__init__(name)
self.health = self.health * 1.5
self.equipment["Weapon"] = self.wep_name # <-- ?

class Weapon(object):
def __init__(self, wep_name):
self.wep_name = wep_name

抱歉,如果标题没有意义。我不太确定这叫什么:(

最佳答案

在包含错误的行中,self 指的是Warrior,而不是Weapon,因此它没有wep_name。如果你想在那时制作一个新的武器,它可能是这样的:

    self.equipment["Weapon"] = Weapon("Sword")

关于python - 将不同的变量分配给不同的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24091061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com