2014-04-30 29 views
0

我試圖製作一個非常簡單的RPG戰鬥系統。變量不會在IF語句中改變

但當衛生低於0和什麼也不做有一個錯誤:

jack_battle_loop_1 = True 
while jack_battle_loop_1: 
    battle_menu() 
    choise = input("> ") 
    if choise == "1": 
     #(jack_health + jack_defense) - my_damage = jack_health 
     jack_health = (jack_health + jack_defense) - random.randrange(20, 25) 
     #Processing the updated enemy health 
     print(deskemon + " HAVE INFLICTED " + str(random.randrange(20, 25)) + " TO CHIPHEAD") 
     print("CHIPHEAD HAS " + str(jack_health) + " HITPOINTS LEFT!") 
     time.sleep(1) 
     print("...") 
     time.sleep(1) 
     print("...") 
     time.sleep(1) 
     print("...") 
     time.sleep(1) 
     #(my_health + my_defense) - jack_damage = my_health 
     my_health = (my_health + my_defense) - jack_damage 
     print("CHIPHEAD HAVE INFLICTED " + str(jack_damage) + " TO " + deskemon) 
     print(deskemon + " HAS " + str(jack_health) + " HITPOINTS LEFT!") 
     jack_battle_loop = True 
    elif jack_health <= 0: 
     jack_battle_loop = False 
    elif my_health <= 0: 
     jack_battle_loop = False 
    elif choise == "": 
     jack_battle_loop = True 

但是,相反,它輸出這樣的:

TIMOHA HAVE INFLICTED 23 TO CHIPHEAD 
CHIPHEAD HAS -24 HITPOINTS LEFT! 
+1

(它的 「選擇」,順便說一句) –

回答

1

elif語句只有choise != "1"執行。看起來檢查健康的if應獨立於檢查選項的if

喜歡的東西

if choice == "1": 
    # do stuff 
if jack_health <= 0 or my_health <= 0: 
    jack_battle_loop = False 
0

後試試這個您如果的choise == 「1」:行:

jacksdamage=random.randrange(20, 25) 
    jack_health = (jack_health + jack_defense) - jacksdamage 
    if jack_health<=0: 
     print "Jack recieved %.i damage and was killed!"%(jacksdamage) 
     break 
    else: 
     print "Jack took %.i damage. from CHIPHEAD."%(jacksdamage) 
     print "Jack has %.i hitpoints left."%(jack_health) 
    for _ in range(5): 
     time.sleep(1) 
     print "..." 
    mydamage=random.randrange(20,25) 
    my_health = (my_health + my_defense) - mydamage 
    if my_health<=0: 
     print deskemon+' took %.i damage from jack and was killed!'%(mydamage) 
     break 
    else: 
     print deskmon+' took %.i damage from jack.'%(mydamage) 
     print deskmon+' has %.i health left.'%(my_health) 
    for _ in range(5): 
     time.sleep(1) 
     print "..."