In the following program, which I am running on Windows 7 professional 64, I am trying to allow the user to intervene if needed (through the inner while
loop) and cause the outer while
loop to repeat an action. Otherwise, the inner while
loop would timeout and the program would just continue unimpeded:
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')
However, the user-input part (pressing ENTER to repeat) of the inner loop is not working and I don't know why. I took its idea from the solution offered here. Any thoughts on how to get this to work?