无法修复我的python游戏中的错误

时间:2013-12-24 20:48:07

标签: python python-3.x

我写了这段代码

hp0 = 100
atk0 = 30
hp1 = 100
atk1 = 5
print("Action? (attack, heal, nothing):")
act = input("> ")
while hp0 > 0 and hp1 > 0:
    if act == "attack":
        hp1 = hp1 - atk0
        if hp1 <= 0:
            print("You won!")
        print("{} DMG".format(atk0)
              )
    if act == "heal":
        hp0 = hp0 + 15
        print("+15 HP")
    else:
        print("...")
    print("Enemy attacked you! -{} hp".format(atk1))
    hp0 = hp0 - atk1
    print("{}HP player".format(hp0))
    print("{}HP enemy".format(hp1))
    print("Action? (attack, heal, nothing):")
    act = input("> ")
if hp0 > 0:
    if hp1 <= 0:
        print("You won!")
if hp1 > 0:
    if hp0 <= 0:
        print("You lose")

这是你(hp0atk0)和“怪物”(hp1atk1)之间的战斗。 当我继续攻击的时候,有一刻敌人的生命(hp1)是-20,它让我在它告诉我赢了之前再做一次动作。

当我决定什么都不做而且我的健康点(hp0)变为0时会发生同样的事情,但它让我再行动一次,我可以决定愈合并继续游戏。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

请求将下一个操作移到循环顶部:

hp0 = 100
atk0 = 30
hp1 = 100
atk1 = 5

while hp0 > 0 and hp1 > 0:
    print("Action? (attack, heal, nothing):")
    act = input("> ")
    if act == "attack":
        hp1 = hp1 - atk0
        if hp1 <= 0:
            print("You won!")
        print("{} DMG".format(atk0)
              )
    if act == "heal":
        hp0 = hp0 + 15
        print("+15 HP")
    else:
        print("...")
    print("Enemy attacked you! -{} hp".format(atk1))
    hp0 = hp0 - atk1
    print("{}HP player".format(hp0))
    print("{}HP enemy".format(hp1))

并删除其他print("Action? ...")input("> ")行。