gpt4 book ai didi

Python - 循环等待输入

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:47 25 4
gpt4 key购买 nike

我浏览了 StackOverflow 论坛,无法找到适用于我的具体问题的解决方案。

我需要编写一些代码,不断检查用户输入。我有一个带有条形码扫描仪的 Raspberry Pi。我希望我的Python脚本循环,等待我的条形码扫描仪发出一些声音(然后它会在事件窗口中“输入”它,它的作用就像键盘一样)。当条形码扫描仪“输入”8 位数字时 - 我需要 Python 脚本停止 - 获取输入并将其保存在变量中。

这是我能想到的唯一伪代码:

// Create variable, store an empty string

// Create a while loop
// Within the while loop, continuously check for input.
// If input has been found, stop the loop and save the input in a variable.

我非常抱歉我无法编写自己的代码 - 我只是不知道从哪里开始。

编辑:扫描仪“输入”数字。但不按 ENTER 键。所以我不知道如何围绕它进行编程。

最佳答案

您可以在 RaspberryPi 上使用此库:https://pypi.python.org/pypi/readchar

import readchar


inputStr = ""

while len(inputStr) != 8:
inputStr += str(readchar.readchar())

# Quote: "Save it in variable"

variable = inputStr

# Clean
inputStr = ""

或者,缩短所有内容:

import readchar

variable = ""
while len(variable) != 8:
variable += str(readchar.readchar())

关于Python - 循环等待输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35020368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com