2012-09-26 30 views
2

循环问题,我试图环战斗菜单。虽然这还不算完呢,我在它的循环问题。我想菜单保持循环,直到myhp,或hp低于0.所以我用“while myhp> 0或hp> 0:”与蟒蛇

它不起作用,我做错了什么?

def fightmode(name, hp, dmg, gold): 
print '\n\n\nYou are in a fight with %s' %name 
print '%s has %sHP' %(name, hp) 
while myhp > 0 or hp > 0: 
    hp = hp - mydmg 
    print '\n\t1. Attack \n\t2. Guard \n\t3. Run away.' 
    opt1= '' 
    allowed = ["1", "2", "3"] 
    while opt1 not in allowed: 
     opt1 = raw_input("\nWhat will you do? ") 
     if opt1 == "1": 
      print "You have inflicted %d damage on %s. %s's HP is %s" %(mydmg, name, name, hp) 
if myhp > 0 : 
    print"myhp" 
if hp > 0 : 
    print"theirhp" 
+0

解释什么是“不工作”的意思。你想要做什么,它做什么呢? – BrenBarn

+0

请重新格式化您的代码,以便将其显示为有效的Python。不要使用标签,而要使用四个空格进行缩进。 – 2012-09-26 06:19:49

回答

2

没关系,我想我明白了。我改“或” to“和”感觉起来像它的工作。 这是正确的方法是什么?

+0

是的,它是。你的while循环需要运行,直到*或者*下降到0以下,而不是直到*两个都做。 –

-1

因为循环截枝,直到布尔表达式为和表达得到后停止

所以你需要写while myhp < 0 or hp < 0:

0

现在你需要myhp OR hp成为真正的while循环继续进行。 所以,如果其中一个下降到0,从而“变虚”,另一个仍然是真实的,并保持循环。

那么你怎么能做些什么? (你已经猜对了...使用and,而不是!)

所以while myhp > 0 or hp > 0:应该while myhp > 0 and hp > 0:(注意orand交换!)