变量在IF语句中不会改变

时间:2014-04-30 16:29:19

标签: python variables if-statement

我试图制作一个非常简单的RPG战斗系统。

但是当健康状况低于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!

2 个答案:

答案 0 :(得分:1)

elif语句仅在choise != "1"时执行。检查健康状况的if似乎应该与检查选择的if无关。

这样的东西
if choice == "1":
    # do stuff
if jack_health <= 0 or my_health <= 0:
    jack_battle_loop = False

答案 1 :(得分:0)

在if if ==“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 "..."