如何停止无限循环

时间:2014-06-02 01:48:00

标签: python loops while-loop

我正在为我班上的作业编写这个简单的游戏。我试图做到这一点,一旦用户输入“是”,它就不再询问他们是否想要前往关东地区。但是如果他们输入no,它会将它们循环回到开头,告诉他们他们首先进入关东地区。

编辑:我如何制作它,以便如果用户输入yes,它会停止循环,如果他输入no,它会继续循环吗?

while gender=='boy' or gender=='Boy':
        #REGION KANTO
        print("We will first proceed to the Kanto Region.")
        print("You are currently facing the Kanto Region Pokemon Master, John!")
        #ATTACK
        attack=input("Would you like to use cut and hurt his Pokemon?")
        if attack=='yes' or attack=='Yes':
                print(starterP,"used cut! Foe Pokemon has fainted!")
                print("You will advance to the next region!")
                print("=====================================================")
        if attack=='no' or attack=='No':
                print(starterP,"got attacked!",starterP,"has fainted!")
                print("Game over!")
                print("=====================================================")        

1 个答案:

答案 0 :(得分:0)

使用break退出while循环:

>>> while True:
...     x+=1
...     if x == 100:
...             print x
...             break
... 
100
>>> 

但是,你的问题有点不清楚。如果我的问题不满意,请发表评论。