【问题标题】:When calling different functions in the same class, only the first function is ever called(Python) [duplicate]在同一个类中调用不同的函数时,只有第一个函数被调用(Python)[重复]
【发布时间】:2016-08-29 16:07:14
【问题描述】:

我的课堂功能有点问题。在我的课堂上,我有 3 个不同的函数,但是每当我在课堂外调用其中一个函数时,尽管我输入了正确的函数名,它只会调用第一个函数。

这是下面的具有不同功能的类,虽然我只包含了两个,因为我不希望您必须搜索大量代码。

class mage(baseclass):
    def __init__(self, name, level, attack, defence, hp):
        baseclass.__init__(self, name, level, hp)
        self.attack = attack
        self.defence = defence
    def __str__(self):
            return "You are now a Mage, your new stats are:\n Level: {0}\n Attack: {1}\n Defence: {2}\n HP: {3}".format(self.level, self.attack, self.defence, self.hp)
    def flamevortex(self, x, y, z):
        print("You used Flame Vortex")
        time.sleep(1.5)
        damageofmove = 3
        damagedone = damageofmove*y
        damagedoneafterdefence = damagedone - z   
        x = x - damagedoneafterdefence
        print("The monster's health is now " + str(x))
        time.sleep(1.5)
        return x
    def lightningbolt(self, x, y, z):
        print("You used Lightning Bolt")
        time.sleep(1.5)
        damageofmove = 3
        damagedone = damageofmove*y
        damagedoneafterdefence = damagedone - z   
        x = x - damagedoneafterdefence
        print("The monster's health is now " + str(x))
        time.sleep(1.5)
        return x

这是我调用函数的地方:

if Userattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX":
                monster1.hp = p1.flamevortex(monster1.hp, p1.attack, monster1.defence)
                if chosenmove == monsterattacks[0]:
                    p1.hp = monsterlasersword(p1.hp)
                elif chosenmove == monsterattacks[1]:
                    p1.hp = monsterswipe(p1.hp)
                elif chosenmove == monsterattacks[2]:
                    monster1.hp = monsterregen(monster1.hp)
                time.sleep(1.5)
                print("After the monster's attacks, your hp is now " + str(p1.hp))
            elif Userattack.upper() == "LIGHTNINGBOLT" or "LIGHTNING BOLT":
                monster1.hp = p1.lightningbolt(monster1.hp, p1.attack, monster1.defence)
                if chosenmove == monsterattacks[0]:
                    p1.hp = monsterlasersword(p1.hp)
                elif chosenmove == monsterattacks[1]:
                    p1.hp = monsterswipe(p1.hp)
                elif chosenmove == monsterattacks[2]:
                    monster1.hp = monsterregen(monster1.hp)
                time.sleep(1.5)
                print("After the monster's attacks, your hp is now " + str(p1.hp))

无论用户输入什么,它只会调用第一个函数。 我知道这需要处理很多,感谢任何帮助。谢谢

【问题讨论】:

    标签: function class python-3.x


    【解决方案1】:

    if Userattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX": 表示userattack.upper() 等于"FLAMEVORTEX",或者字符串"FLAME VORTEX" 是否具有True 值。

    现在因为空字符串是 False 而非空字符串是 TrueUserattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX" 总是 True,这不是你的意思。

    试试:Userattack.upper() == "FLAMEVORTEX" or Userattack.upper()=="FLAME VORTEX"

    【讨论】:

    • if Userattack.upper() in ["FLAMEVORTEX", "FLAME VORTEX"]if Userattack.upper() in ["FLAMEVORTEX", "FLAME VORTEX"] 是一种更惯用、更易读的方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 2016-12-05
    • 2011-05-02
    • 1970-01-01
    相关资源
    最近更新 更多