如何读取键盘输入?
我想用Python从键盘读取数据。我尝试了这段代码:
nb = input('Choose a number')
print('Number%s \n' % (nb))
但它不起作用,无论是在 Eclipse 还是在终端中,它总是停止问题。我可以输入一个数字,但什么也没发生。
你知道为什么吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
则使用
如果您使用Python 3,
。如果您想要一个数值,只需将其转换即可:
如果您使用Python 2,则需要使用
raw_input
而不是input
。Use
if you use Python 3.
And if you want to have a numeric value, just convert it:
If you use Python 2, you need to use
raw_input
instead ofinput
.看来你在这里混合了不同的Python(Python 2.x vs. Python 3.x)......
这基本上是正确的:
问题是它仅在 Python 3 中受支持。正如 @sharpner 回答的那样,对于旧版本的 Python (2.x),您必须使用函数
raw_input
:如果您想要将其转换为数字,那么你应该尝试:
...尽管你需要考虑到这可能会引发异常:
如果你想使用格式打印数字,在Python 3
str.format建议使用 ()
:而不是:
但这两个选项(
str.format()
和%
)都可以在 Python 2.7 和 Python 3 中使用。It seems that you are mixing different Pythons here (Python 2.x vs. Python 3.x)...
This is basically correct:
The problem is that it is only supported in Python 3. As @sharpner answered, for older versions of Python (2.x), you have to use the function
raw_input
:If you want to convert that to a number, then you should try:
... though you need to take into account that this can raise an exception:
And if you want to print the number using formatting, in Python 3
str.format()
is recommended:Instead of:
But both options (
str.format()
and%
) do work in both Python 2.7 and Python 3.非阻塞、多线程示例:
由于阻塞键盘输入(因为
input()
函数会阻塞)通常不是我们想要做的事情(我们经常会这样做)喜欢继续做其他事情),这里有一个非常精简的多线程示例来演示如何继续运行您的主应用程序,同时在键盘输入到达时仍然读取它们。我在我的 eRCaGuy_PyTerm 串行终端程序 此处(在代码中搜索input()
)。这是通过创建一个在后台运行的线程,不断调用
input()
并将其接收到的任何数据传递到队列来实现的。这样,您的主线程就可以做任何它想做的事情,只要队列中有东西,就从第一个线程接收键盘输入数据。
1. 裸 Python 3 代码示例(无注释):
2. 与上面相同的 Python 3 代码,但带有大量解释性注释:
示例输出:
Python Queue 库是线程安全的:
请注意,
Queue.put()
和Queue.get()
以及其他 Queue 类方法都是线程安全的! (这与 C++ 标准模板库中的队列和其他容器不同!)由于 Python Queue 类及其方法是线程安全的,这意味着它们实现了交互所需的所有内部锁定语义。线程操作,因此队列类中的每个函数调用都可以被视为单个原子操作。请参阅文档顶部的注释: https://docs.python.org/ 3/library/queue.html(强调):参考文献:
相关/交叉链接:
Non-blocking, multi-threaded example:
As blocking on keyboard input (since the
input()
function blocks) is frequently not what we want to do (we'd frequently like to keep doing other stuff), here's a very-stripped-down multi-threaded example to demonstrate how to keep running your main application while still reading in keyboard inputs whenever they arrive. I use this technique in my eRCaGuy_PyTerm serial terminal program here (search the code forinput()
).This works by creating one thread to run in the background, continually calling
input()
and then passing any data it receives to a queue.In this way, your main thread is left to do anything it wants, receiving the keyboard input data from the first thread whenever there is something in the queue.
1. Bare Python 3 code example (no comments):
2. Same Python 3 code as above, but with extensive explanatory comments:
Sample output:
The Python Queue library is thread-safe:
Note that
Queue.put()
andQueue.get()
and other Queue class methods are all thread-safe! (This is unlike queues and other containers in the standard template library in C++!) Since the Python Queue class and its methods are thread-safe, that means they implement all the internal locking semantics required for inter-thread operations, so each function call in the queue class can be considered as a single, atomic operation. See the notes at the top of the documentation: https://docs.python.org/3/library/queue.html (emphasis added):References:
Related/Cross-Linked:
我来这里是为了寻找如何阅读单个字符。
我根据 readchar 库stackoverflow.com/questions/510357/how-to-read-a-single-character-from-the-user">这个问题。 pip 安装后:
I came here looking for how to read a single character.
I found the readchar library, based on the answers to this question. After pip install:
您可以通过使用变量来简单地使用 input() 函数。快速的例子!
you can simply use the input() function by using a variable. quick exemple!