Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python getch()在打印之前接受输入_Python - Fatal编程技术网

Python getch()在打印之前接受输入

Python getch()在打印之前接受输入,python,Python,我试图打印一条消息,然后使用下面的代码段从用户那里获取一个字符作为输入 import getch print("Enter a character: ", end="") char = getch.getch() print("You entered", char) 但出于某些原因,它首先要求输入。在给出输入后,它显示消息。如何修复此问题?这是因为没有刷新打印缓冲区。试试这个: import getch print("E

我试图打印一条消息,然后使用下面的代码段从用户那里获取一个字符作为输入

import getch

print("Enter a character: ", end="")
char = getch.getch()
print("You entered", char)

但出于某些原因,它首先要求输入。在给出输入后,它显示消息。如何修复此问题?

这是因为没有刷新打印缓冲区。试试这个:

import getch

print("Enter a character: ", end="", flush=True)
char = getch.getch()
print("You entered", char)

成功了。谢谢对不起,如果我听起来很愚蠢,但什么是打印缓冲区?一些有用的阅读非常感谢你。