[英]How do I end an infinite loop in python if no input(or the enter key is pressed) is provided in the loop
[英]Python - how to continue a loop with absolutely no input, including enter key stroke
ayy = 0
while True:
ayy = ayy + 1
print(Randoz(a,b,c,d))
if ayy % 3 == 0:
omg = input('Do you wish to stop the loop?, if yes type yes, if not, type no')
if omg == 'yes':
break
if omg == '':
ayy = ayy
我正在嘗試制作一個每3圈停下來的循環,並詢問用戶是否要中斷,並可以選擇不做任何事情並繼續循環,我似乎無法弄清楚如何在用戶不做的情況下繼續循環任何事情,我都需要按Enter以便進行另一個循環。
randoz是我創建的函數,無關緊要。
您可以使用以下代碼啟動一個循環,該循環將永遠運行,並且僅當用戶在每次第三次迭代中提示輸入時,才在用戶添加'y'
或'Y'
時退出
import itertools
for i in itertools.count(1):
print(Randoz(a,b,c,d))
if i % 3 == 0:
quit = input('Do you want to quit? y/n')
if quit.lower() == 'y':
break
輸出示例並打印迭代:
1
2
3
Do you want to quit? y/n
4
5
6
Do you want to quit? y/n
7
8
9
Do you want to quit? y/n
10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.