类中的属性会立即在错误的时间运行吗?

时间:2016-07-18 05:23:33

标签: python

我是Python的新手,我正在通过创建基于文本的游戏来学习类和方法的交互方式。代码运行得很好,但是,我的问题在于Name(object)类。无论我把它放在代码中,(如下所示,在开头或中间)我将始终提示输入。我怀疑可能是因为name_new属性提示raw_input(&#34;&gt;&#34;),但是,我想知道在提示类名()时是否有办法防止或改变。< / p>

class Character(object):

    def __init__(self, name, weapon, spec_ability, health):
        self.name = name
        self.weapon = weapon
        self.spec_ability = spec_ability
        self.health = 100


    def special_ability(self):
        special = False
        rand_num = str(random.choice(rdm))

        if '1' in rand_num or '2' in rand_num or '3' in rand_num:
            special = True
            if special:
                print "Your special ability is ready."
                return True

            else:
                return
        else:
            print "Your special ability is not ready."

    def Critical_Chance(self):
        crit = False
        rand_num = str(random.choice(rdm))


        if '1' in rand_num or '2' in rand_num or '3' in rand_num:
            crit = True
            if crit:
                print "Critical hit incoming."
            return True
        else: 
            return

class Warrior(Character):

    def __init__(self, name, weapon, spec_ability, health, armor):
        super(Warrior, self).__init__(name, weapon, spec_ability, health)
        self.armor = armor

class Mage(Character):

    def __init__(self, name, weapon, spec_ability, health, cloak):
        super(Mage, self).__init__(name, weapon, spec_ability, health)
        self.cloak = cloak

class Rogue(Character):

    def __init__(self, name, weapon, spec_ability, health, boots):
        super(Rogue, self).__init__(name, weapon, spec_ability, health)

class Name(object):

    name_new = raw_input("> ")


def start():

    print """\nWelcome to the world of _______. \n
You are a young hero or heroine
in search of the gold that hides within the Mountains of _____.
In order to obtain the gold and treasure that lies within the
mountains, you must battle great monsters and face dangerous 
perils."""

    print "Will you pursue such adventure? Or will you refuse?"

    choice = raw_input("> ")

    if "Yes" in choice or "yes" in choice: 
        Introduction()
    elif "No" in choice or "no" in choice:
        sys.exit()
    else:
        print "You typed: %s. \nPlease try again." % choice
        start()


def Introduction():

    print "Thank you for joining in on this adventure..." 
    print "Well, welcome to your adventure for the gold."
    print "You will encounter dangerouse quests and enemies, but you will be\
rewarded in the end."

    print "Now, what class will you be?\n\
Warrior                 Mage                Rogue"

    char_choice = raw_input("> ")

    verynew_name = Name()
    name_choice = Name.name_new

    if "Warrior" in char_choice or "warrior" in char_choice:
        name_choice = Warrior(name_choice, None, None, None, None)
        print name_choice.name 
        print name_choice.weapon
        print name_choice.spec_ability
        print name_choice.health
        print name_choice.armor 


    elif "Mage" in char_choice or "mage" in char_choice:
        name_choice = Mage(name_choice, None, None, None, None)
        print name_choice.name 
        print name_choice.weapon
        print name_choice.spec_ability
        print name_choice.health
        print name_choice.cloak

    elif "Rogue" in char_choice or "rogue" in char_choice:
        name_choice = Rogue(name_choice, None, None, None, None)
        print name_choice.name 
        print name_choice.weapon
        print name_choice.spec_ability
        print name_choice.health
        print name_choice.boots 

    else:
        print "You must pick a class."
        start()

    print "Our story starts in the town of Larton."
    #super(Larton, self).enter()


start()

2 个答案:

答案 0 :(得分:2)

运行程序时,将执行模块级代码。这包括您的Name课程。

class Name(object):
    name_new = raw_input("> ")

这会创建类Name,并且也会在类中执行代码。这就是您提示输入姓名的原因。

如果您希望能够提示用户输入此类名称

verynew_name = Name()

您必须更改Name课程,以便在实例化课程时执行raw_input(因为这是Name()所做的事情,它创建了Name)的实例:

class Name(object):
    def __init__(self):
        self.name_new = raw_input("> ")

(请注意,您必须使用name_choice = Name.name_new而不是name_choice = verynew_name.name_new

答案 1 :(得分:1)

定义Character类的构造函数和不将名称作为参数的子类: e.g。

阶级勇士(角色):

def __init__(self, weapon, spec_ability, health, armor):
    super(Warrior, self).__init__(weapon, spec_ability, health)
    self.armor = armor

并且在字符类中,您可以将self.name默认定义为None

您可以随后使用类似

之类的内容在逻辑中指定名称
character.name = Name.name_new

只要你在实际读取name的值之前就这样做了,你就没事了