简体   繁体   English

Python:随时接受用户输入

[英]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.

相关问题 如何打印倒数计时器并同时接受用户输入(python)? - How to print countdown timer and accept user input at the same time (python)? 随时使用Python捕获用户输入 - Python Catching user input at any time Python 接受预定义的输入以及用户输入中的任何输入并保存为变量 - Python accept predefined input along with any input inside of a user input and save as variable 如何在不等待Python的情况下接受用户输入 - How to accept user input without waiting in Python 如何显示倒数计时器并同时接受用户输入(python)? - How do you display a countdown timer and accept user input at the same time (python)? 如何在Python的多维列表中接受用户输入的字符串? - How to accept user input string in multi dimension list in Python? 如何通过单击 Python 中的 Tkinter 按钮接受图像作为用户输入? - How to accept image as user input by clicking on Tkinter button in Python? 接受来自输入用户的带有“/”的数字,当它是 python 中的字母时会出现异常 - Accept number with “/” from input user and exception when it is a letter in python Python如何只接受用户输入的某些单词 - Python How to accept only certain words with user input 如何在Python的多维列表中接受用户输入? - How to accept user input in multi dimension list in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM