Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
要在Python循环中重复的超时或用户输入_Python_Windows_Loops_Timeout_Controls - Fatal编程技术网

要在Python循环中重复的超时或用户输入

要在Python循环中重复的超时或用户输入,python,windows,loops,timeout,controls,Python,Windows,Loops,Timeout,Controls,在我在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

在我在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')
导入msvcrt
导入时间
决策='不重复'#默认设置
对于['f1','f2','f3']中的f:
打印('在f'上执行一些准备操作')
while True:#外部while循环允许在f上重复操作
打印('在f上执行更多操作')
t0=时间。时间()
while time.time()-t0<10:#内部while循环允许用户干预
if msvcrt.kbhit():#并按ENTER if重复操作
如果msvcrt.getch()
decision=“repeat”
打破
其他:
打破
睡眠时间(0.1)
如果决策==“重复”:
打印(“在外部while循环中重复f…”)
持续
其他:
打破
打印('在for循环中对f执行最终操作')
但是,内部循环的用户输入部分(按ENTER键重复)不工作,我不知道为什么。我从提供的解决方案中得到了它的想法。
关于如何使其工作,您有什么想法吗?

由于使用==运算符,您正在比较内部循环中的变量决策和字符串“repeat”。应使用=为变量赋值:

decision = 'repeat'

我现在已经设法解决了这个问题。
kbhit
进程在我正在使用的IDLE(Wing IDE)中不起作用,但如果从命令提示符调用,则可以起作用(正如@eryksun所说,这可能适用于所有IDLE,而不仅仅是Wing)。我发现的另一个问题是
getch()
进程不能满足我的需要,我必须使用返回unicode的
getwch()
。再进行一次微调(默认值为
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')
导入msvcrt
导入时间
决策='不重复'#默认设置
对于['f1','f2','f3']中的f:
打印('在f'上执行一些准备操作')
while True:#外部while循环允许在f上重复操作
打印('在f上执行更多操作')
t0=时间。时间()
while time.time()-t0<10:#内部while循环允许用户干预
if msvcrt.kbhit():#并按ENTER if重复操作
如果msvcrt.getchw()='\r':#需要或允许超时继续
decision=“repeat”
打破
其他:
打破
睡眠时间(0.5)
如果决策==“重复”:
打印(“在外部while循环中重复f…”)
决策='将决策重置为不重复'
持续
其他:
打破
打印('在for循环中对f执行最终操作')

感谢您注意到这一点。我修复了它,但它与内部循环没有关系。我还将
for
循环设置为字符串,以允许代码运行。您确定
msvcrt.getch()
返回“\r”?这似乎是在Windows上按ENTER键时预期返回的结果。这就是我借用的代码。可能是时间问题,因为其他一切看起来都正常。请尝试逐个调试内部循环中的代码步进行。我不在电脑中,所以现在无法测试。谢谢。我已完成了调试。
msvcrt.kbhit()
即使按下键也不会返回
True
kbhit
getch
要求将进程连接到控制台窗口。如果使用IDLE,则进程没有控制台——至少在使用pythonw.exe以默认方式运行时没有。即使使用连接的控制台以IDLE方式运行(例如,使用Win+R运行对话框运行
py-3-m idlelib
),我怀疑您是否希望用户必须切换到控制台窗口才能输入。无论如何,IDLE和其他IDE外壳只是开发环境。如果您打算将其作为控制台脚本,则可以模拟假控制台I/O函数,以便在没有连接控制台的情况下(例如,
打开(“CONIN$”
失败)进行测试)。如果不是控制台程序,则使用GUI工具包创建自己的窗口并读取键盘输入。