【问题标题】:How to stop a infinite while loop如何停止无限循环
【发布时间】:2014-06-02 01:47:59
【问题描述】:

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

编辑:我将如何做到这一点,如果用户输入是,它会停止循环,如果他输入否,它会继续循环?

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("=====================================================")        

【问题讨论】:

  • break 会破坏它,但您的真正问题是什么?
  • 使用break 声明
  • 你无法摆脱无限循环

标签: python loops while-loop


【解决方案1】:

使用break 退出while循环:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 2021-12-05
    相关资源
    最近更新 更多