手动打破循环

问题描述 投票:0回答:1

我正在使用一个我想无限循环的函数构建一个 while 循环。但我希望能够在输入某些内容时结束循环。如何创建一个无限循环,当你告诉它时它就会中断?

import time
islooping = True
counter = 1

while islooping == True:
    print("stillgoing " + str(counter))
    counter += 1
    time.sleep(2)
python loops while-loop
1个回答
0
投票
import time
islooping = True
counter = 1

while islooping == True:
    print("stillgoing " + str(counter))
    input1 = input("Please add your input or type 'exit' to break the loop: ")
    if input1.lower() == "exit":
        break
    counter += 1
    time.sleep(2)

输出:

© www.soinside.com 2019 - 2024. All rights reserved.