超时或用户输入在 Python 循环中重复
Timeout or User Input to Repeat in Python Loops
在下面的程序中,我是 运行 在 Windows 7 professional 64 上,我试图让用户在需要时进行干预(通过内部 while
循环)并导致外部 while
循环重复一个动作。否则,内部 while
循环会超时,程序会继续畅通无阻:
import msvcrt
import time
decision = 'do not repeat' # default setting
for f in ['f1', 'f2', 'f3']:
print ('doing some prepartory actions on f')
while True: # outer while loop to allow repeating actions on f
print ('doing some more actions on f')
t0 = time.time()
while time.time() - t0 < 10: # inner while loop to allow user to intervene
if msvcrt.kbhit(): # and repeat actions by pressing ENTER if
if msvcrt.getch() == '\r': # needed or allow timeout continuation
decision = "repeat"
break
else:
break
time.sleep(0.1)
if decision == "repeat":
print ("Repeating f in the outer while loop...")
continue
else:
break
print ('doing final actions on f in the for loop')
但是,内部循环的用户输入部分(按 ENTER 重复)不起作用,我不知道为什么。我从提供的解决方案 here 中得到了它的想法。
关于如何让它发挥作用有什么想法吗?
您在内部循环中比较 变量决策和字符串 "repeat",因为您使用的是 == 运算符。您应该使用 = 来为变量赋值:
decision = 'repeat'
我现在已经设法解决了这个问题。 kbhit
进程在我使用的 IDLE 中不起作用(Wing IDE),但如果从命令提示符调用则可以工作(正如@eryksun 所说,这可能适用于所有 IDLE,而不仅仅是翅膀)。我发现的另一个问题是 getch()
进程没有做我需要的,我必须使用 getwch()
which returns unicode。再进行一次小调整(默认 decision
和 decision = 'Reset decision to not repeat'
,代码现在处于良好的工作状态:
import msvcrt
import time
decision = 'do not repeat' # default setting
for f in ['f1', 'f2', 'f3']:
print ('doing some prepartory actions on f')
while True: # outer while loop to allow repeating actions on f
print ('doing some more actions on f')
t0 = time.time()
while time.time() - t0 < 10: # inner while loop to allow user to intervene
if msvcrt.kbhit(): # and repeat actions by pressing ENTER if
if msvcrt.getchw() == '\r': # needed or allow timeout continuation
decision = "repeat"
break
else:
break
time.sleep(0.5)
if decision == "repeat":
print ("Repeating f in the outer while loop...")
decision = 'Reset decision to not repeat'
continue
else:
break
print ('doing final actions on f in the for loop')
在下面的程序中,我是 运行 在 Windows 7 professional 64 上,我试图让用户在需要时进行干预(通过内部 while
循环)并导致外部 while
循环重复一个动作。否则,内部 while
循环会超时,程序会继续畅通无阻:
import msvcrt
import time
decision = 'do not repeat' # default setting
for f in ['f1', 'f2', 'f3']:
print ('doing some prepartory actions on f')
while True: # outer while loop to allow repeating actions on f
print ('doing some more actions on f')
t0 = time.time()
while time.time() - t0 < 10: # inner while loop to allow user to intervene
if msvcrt.kbhit(): # and repeat actions by pressing ENTER if
if msvcrt.getch() == '\r': # needed or allow timeout continuation
decision = "repeat"
break
else:
break
time.sleep(0.1)
if decision == "repeat":
print ("Repeating f in the outer while loop...")
continue
else:
break
print ('doing final actions on f in the for loop')
但是,内部循环的用户输入部分(按 ENTER 重复)不起作用,我不知道为什么。我从提供的解决方案 here 中得到了它的想法。 关于如何让它发挥作用有什么想法吗?
您在内部循环中比较 变量决策和字符串 "repeat",因为您使用的是 == 运算符。您应该使用 = 来为变量赋值:
decision = 'repeat'
我现在已经设法解决了这个问题。 kbhit
进程在我使用的 IDLE 中不起作用(Wing IDE),但如果从命令提示符调用则可以工作(正如@eryksun 所说,这可能适用于所有 IDLE,而不仅仅是翅膀)。我发现的另一个问题是 getch()
进程没有做我需要的,我必须使用 getwch()
which returns unicode。再进行一次小调整(默认 decision
和 decision = 'Reset decision to not repeat'
,代码现在处于良好的工作状态:
import msvcrt
import time
decision = 'do not repeat' # default setting
for f in ['f1', 'f2', 'f3']:
print ('doing some prepartory actions on f')
while True: # outer while loop to allow repeating actions on f
print ('doing some more actions on f')
t0 = time.time()
while time.time() - t0 < 10: # inner while loop to allow user to intervene
if msvcrt.kbhit(): # and repeat actions by pressing ENTER if
if msvcrt.getchw() == '\r': # needed or allow timeout continuation
decision = "repeat"
break
else:
break
time.sleep(0.5)
if decision == "repeat":
print ("Repeating f in the outer while loop...")
decision = 'Reset decision to not repeat'
continue
else:
break
print ('doing final actions on f in the for loop')