While循环和函数中的if语句。在进入if语句之前停止

时间:2019-10-14 00:20:11

标签: python function if-statement while-loop

“基于文本的故事/冒险”

我只需要战斗就可以结束并执行其他功能,具体取决于hp等级== 0的玩家或土匪。我有一个名为Player()的类,p1和土匪是该类的两个单独的实例,它们的开头是hp为2。“ self.hp = 2”

我想指出的是,匪徒是故事中的“两个”人,但他们构成了“玩家”类中的“一个”实例。那是故意的。这个故事不应该给您带来太大的挑战。我可以使用一些帮助来超越战斗。

我在控制台中得到这个:

Early in the morning, you are startled so bad you shoot up out of your blanket!
You hear a ruckus in the wagon next to you and find a couple of bandits
sneaking off with the food the whole wagon train eats from.

You've got to do something fast. There are only two of em, and they look pretty scrawny.

[A] Fight like a man. Save the food supply.
[B] Holler for help from the nice lady in the wagon over yonder... then run away.
[M] View Map
[I] View Bag
*************************
What are you gonna do?
 >a
 --- FIGHT THE BANDITS! --- 
1) Left hook
2) Right hook
3) Spur em with your boots!


Pick a move
> 3
You got em good. Both bandits are lookin' skeerd now!
 --- FIGHT THE BANDITS! --- 
1) Left hook
2) Right hook
3) Spur em with your boots!


Pick a move
> 2
You got em good. Both bandits are lookin' skeerd now!

Process finished with exit code 0

这是代码:如何继续下一个场景。

### FIGHT FUNCTION ### (TRIGGER UNLOCK) ###
def fight():
    if p1.hp == 0:
        bandits_won
    elif bandits.hp == 0:
        p1_won
    else:
        pass
    while p1.hp > 0 and bandits.hp > 0:
        print(" --- FIGHT THE BANDITS! --- ")
        print("1) Left hook\n2) Right hook\n3) Spur em with your boots!\n\n")
        act = int(input("Pick a move\n> "))
        if act in range(1,4):
            bandits.hp -= 1
            print("You got em good. Both bandits are lookin' skeerd now!")
            continue
        else:
            print("That wasn't an option bozo! You got a knuckle sandwich and lost [1] HP!")
            p1.hp -= 1
            print("You have ", p1.hp, " HP left! Don't miss again!!!")
            continue
    p1_won = False
    bandits_won = False
    if p1_won == True:
        print("You got them suckers. The wagon train is grateful. You grab a bite for your trouble.")
        p1.hp = 2
        scene3()
    elif bandits_won == True:
        delay_print("They knocked you out cold, took the food, and the whole wagon train suffered. Croup took the "
                    "child...\n")
        delay_print("You failed your mission. No sense in going on now!")
        main_menu()

1 个答案:

答案 0 :(得分:0)

在我看来,您似乎从未将值True分配给变量p1_won或bandits_won。将此代码移到while循环下方。

if p1.hp == 0:
        bandits_won = True
    elif bandits.hp == 0:
        p1_won = True
    else:
        pass

您还可以将if p1_won == True:简化为if p1_won:,并简化elif语句