Python 当我使用%时,为什么这里会出现操作数类型错误?
我正在制作一个基于文本的RPG,我已经在这上面呆了至少一个星期了 这是我创造的敌人职业,我现在的功能是攻击Python 当我使用%时,为什么这里会出现操作数类型错误?,python,python-2.7,Python,Python 2.7,我正在制作一个基于文本的RPG,我已经在这上面呆了至少一个星期了 这是我创造的敌人职业,我现在的功能是攻击 class enemy: def __init__(self,name,level,health): self.name = name self.level = level self.health = health def attack(self): print "A %r appears! It wants
class enemy:
def __init__(self,name,level,health):
self.name = name
self.level = level
self.health = health
def attack(self):
print "A %r appears! It wants to fight!" % (self.name)
player.weapon = (raw_input("What do you attack with? >>").lower())
while (player.health > 0) or (self.health > 0):
if (player.inventory.get(player.weapon) > 0):
player.health = player.health - ( ( randint(0,5) ) + attack_multiplier(self.level) )
print "%r strikes! Your health is down to %r" %(self.name, player.health)
if (player.health > 0) and (self.health > 0):
if weapon_probability() == "critical hit":
self.health -= (((randint(0,5))) + (attack_multiplier(weapon_levels.get(player.weapon))) * 2)
print_slow( "Critical Hit!")
elif weapon_probability() == "hit":
self.health -=((((randint(0,5))) + (attack_multiplier(weapon_levels.get(player.weapon)))))
print_slow( "Hit!")
elif weapon_probability() == "miss":
print_slow( "Miss")
print_slow("Enemy health down to %r !") % self.health
elif player.health <= 0:
print_slow("Your health...it's falling")
break
elif self.health <= 0:
print_slow( "Enemy vanquished!")
break
else:
print "You don't have that!"
player.weapon = (raw_input("What do you attack with? >>").lower())
谢谢你的帮助,我被困在这个问题上太久了,真的很烦人。我觉得解决方案会很简单,但我不知道该怎么做将
%self.health
放入Paranthesis中将%self.health
放入Paranthesis中您需要将%
操作符应用于字符串,而不是print\u slow()的返回值。该函数返回None
,并且None%self.health
引发错误
改变
print_slow("Enemy health down to %d !") % self.health
到
注意右括号的位置。您的代码将%
应用于错误的对象。您需要将%
运算符应用于字符串,而不是print\u slow()
的返回值。该函数返回None
,并且None%self.health
引发错误
改变
print_slow("Enemy health down to %d !") % self.health
到
注意右括号的位置。您的代码将%
应用于错误的对象
print_slow("Enemy health down to %d !" % self.health)