2012-09-26 167 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交換!)