Function 在同一类中调用不同函数时,只调用第一个函数(Python)
我的类函数有点问题。在我的类中,我有3个不同的函数,但每当我在类外调用其中一个函数时,它只调用第一个函数,尽管我输入了正确的函数名 下面是一个具有不同函数的类,尽管我只包含了两个,因为我不希望您必须搜索大量代码Function 在同一类中调用不同函数时,只调用第一个函数(Python),function,class,python-3.x,Function,Class,Python 3.x,我的类函数有点问题。在我的类中,我有3个不同的函数,但每当我在类外调用其中一个函数时,它只调用第一个函数,尽管我输入了正确的函数名 下面是一个具有不同函数的类,尽管我只包含了两个,因为我不希望您必须搜索大量代码 class mage(baseclass): def __init__(self, name, level, attack, defence, hp): baseclass.__init__(self, name, level, hp) self.
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))
不管用户输入什么,它只调用第一个函数。
我知道这是一个很大的过程,并感谢任何帮助。感谢如果Userattack.upper()==“FLAMEVORTEX”或“FLAME VORTEX”:
表示是Userattack.upper()
等于“FLAMEVORTEX”
,或者字符串“FLAME VORTEX”
是否具有True
值
现在,由于空字符串为False,非空字符串为True
,Userattack.upper()==“FLAMEVORTEX”或“FLAMEVORTEX”
始终为True
,这不是您的意思
尝试:Userattack.upper()=“FLAMEVORTEX”或Userattack.upper()=“FLAME VORTEX”如果Userattack.upper()=“FLAMEVORTEX”或“FLAME VORTEX”:
表示是Userattack.upper()
等于“FLAMEVORTEX”
,或者字符串“FLAME VORTEX”
是否具有True
值
现在,由于空字符串为False,非空字符串为True
,Userattack.upper()==“FLAMEVORTEX”或“FLAMEVORTEX”
始终为True
,这不是您的意思
尝试:Userattack.upper()==“FLAMEVORTEX”或Userattack.upper()==“FLAME VORTEX”
如果使用Userattack,则更惯用、更可读的方法是。如果使用Userattack,则更惯用、更可读的方法是