raw_input 在要求输入后打印提示 [英] raw_input prints prompt after asking for input
问题描述
当我使用 raw_input 时,提示仅在用户输入后显示.像这样:
When I use raw_input, the prompt only shows after the user gives input. Like this:
number = raw_input("Enter a number:")
但是当我运行这个时,没有任何反应,我输入一个数字,它显示提示:
but when I run this, nothing happens, the I type a number, the it shows the prompt:
123
Enter a number:
(123 以前是空白的,直到我输入一个数字并按回车键)
(123 used to be blank until I typed a number and hit enter)
我只想在用户输入之前显示提示.如果有人知道如何解决这个问题,请帮忙.
I just want the prompt to display before the user input. If anybody knows how to fix this please help.
谢谢.
推荐答案
感谢您的建议,我确实尝试过,但不幸的是它不起作用,但我确实找到了解决方案:
Thanks for you suggestion, and I did try that, but unfortunately it didn't work, but I did find the solution:
我必须添加
sys.stdout.flush()
之前每次我有一个
variable = raw_input("A prompt")
刷新缓冲区.
虽然是第一次
raw_input("A prompt")
除非你已经打印了一些东西,否则它不会工作,例如
it will not work work unless you already printed something, for example
variable = raw_input("A prompt")
sys.stdout.flush()
仍然会有同样的问题,而
would still have the same issue, whereas
print"Welcome,"
variable = raw_input("A prompt")
sys.stdout.flush()
会起作用.
这篇关于raw_input 在要求输入后打印提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!