'character_Creation'对象没有属性'choice'

时间:2014-02-02 19:08:25

标签: python

我一直收到这个错误'Character_Creation'对象没有属性'choice'但是我明确地将choice定义为,choice = input(“>”)并创建了对象角色。有关如何解决此问题的任何帮助吗?

class Character_Creation():

def __init__(self):

    print("""Character Creation

Whats your name?""")

    Name = input(">")
    print("""Chose your career
          Type b for Blacksmith, a weapon-smith
          Type bo for BodyBuilder, a Russian strong man
          Type m for Merchant , a man with strong trade skills
          """)
    choice = input(">")

class Classes():
def Blacksmith():
    health = 100
    attack = 30
    defence = 10
    money = 100
def Bodybuilder():
    health = 150
    attack = 10
    defense = 30
    money = 100
def Merchant():
    health = 100
    attack = 5
    defence = 5
    money = 500

class Stats():
def __init__(self):
    if c.choice == "m":
        cl.Merchant()
        print(attack)











t=Title_Screen()
c=Character_Creation()
cl=Classes()
s=Stats()

1 个答案:

答案 0 :(得分:0)

JBernardo是正确的,你没有正确使用self。但更广泛地说,你对类的使用有点奇怪。 CharacterCreation似乎更适合于类方法而不是整个类,并且使用具有定义然后丢弃局部变量的实例方法的类Classes有点奇怪。我想你想要做的看起来更像是:

class Character(object):

    def __init__(self, name, health, attack, defence, money):
        self.name = name
        self.health = health
        self.attack = attack
        self.defence = defence
        self.money = money

    @classmethod
    def create(cls, name):
        characters = {"bodybuilder": (150, 10, 30, 100), ...}
        return cls(name, *characters[name])

...    

if choice.lower() == "bo":
    character = Character.create("bodybuilder")

如果您需要不同类型的Character来拥有不同的实例方法和属性,请使用继承:

def Blacksmith(Character):

    def hammer_metal(self):