Python:随时接受用户输入 [英] Python: Accept user input at any time

查看:317
本文介绍了Python:随时接受用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个可以执行许多操作的单元,其中一个计数机器的周期.当我将其转移到梯形图逻辑(CoDeSys)时,我首先将想法融入Python.

I am creating a unit that will do a number of things, one of them counting cycles of a machine. While I will be transferring this over to ladder logic (CoDeSys), I am putting my ideas into Python first.

我将运行一个计数,只需一个简单的

I will have a count running, with just a simple

    counter += 1
    print("counter")

跟踪我正在进行的周期.但是,我希望能够随时重置此计数,最好输入"RESET",我了解如何使用输入命令,

to keep track of what cycle I'm on. However, I want to be able to reset this count at any time, preferably by typing "RESET" I understand how to use the input command,

    check = input()

但是,我不知道如何在搜索输入时让程序运行,或者这是否完全可能.预先感谢您的答复.

however, I do not know how to let the program run while it is searching for an input, or whether or not this is possible at all. Thank you in advance for any answer.

如果有帮助,请看这里的代码.最大的差距是问题所在. http://pastebin.com/TZDsa4U4

If it helps to understand, here is the code. The big gap is where the problem is. http://pastebin.com/TZDsa4U4

推荐答案

如果您只想发出计数器重置的信号,则可以捕获

If you only want to signal a reset of the counter, you can catch KeyboardInterrupt exception.

while True:
    counter = 0
    try:
        while True:
            counter += 1
            print("counter")
    except KeyboardInterrupt:
        pass

这篇关于Python:随时接受用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆