[英]Python: Accept user input at any time
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.
虽然我将把它转移到梯形逻辑(CoDeSys),但我首先将我的想法引入Python。
I will have a count running, with just a simple 我会有一个计数运行,只是一个简单的
counter += 1
print("counter")
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,
但是,我希望能够随时重置此计数,最好输入“RESET”我理解如何使用输入命令,
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.
提前感谢您的回答。
If it helps to understand, here is the code. 如果它有助于理解,这里是代码。 The big gap is where the problem is.
差距就是问题所在。 http://pastebin.com/TZDsa4U4
http://pastebin.com/TZDsa4U4
If you only want to signal a reset of the counter, you can catch KeyboardInterrupt exception. 如果您只想发出计数器重置信号,则可以捕获KeyboardInterrupt异常。
while True:
counter = 0
try:
while True:
counter += 1
print("counter")
except KeyboardInterrupt:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.