Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 蟒蛇3:使用;选择。选择";带打印(str,end=';';)_Python_Python 3.x - Fatal编程技术网

Python 蟒蛇3:使用;选择。选择";带打印(str,end=';';)

Python 蟒蛇3:使用;选择。选择";带打印(str,end=';';),python,python-3.x,Python,Python 3.x,我使用select.select()而不是input,因为我希望输入超时。我在print()函数中使用“end”参数,因为我希望我的终端有这样一行: 键入>在此处键入内容 相反,在键入字符串并按enter键之前,我不会看到“Type>” 我的代码: #!/usr/bin/env python3 # -*- coding: utf-8 -*- #Made by Devyn Collier Johnson, NCLA, Linux+, LPIC-1, DCTS import sys, select

我使用select.select()而不是input,因为我希望输入超时。我在print()函数中使用“end”参数,因为我希望我的终端有这样一行:

键入>在此处键入内容

相反,在键入字符串并按enter键之前,我不会看到“Type>”

我的代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Made by Devyn Collier Johnson, NCLA, Linux+, LPIC-1, DCTS
import sys, select

print('Type > ', end=" ")
INPUT, VOID0, VOID1 = select.select([sys.stdin], [], [], 3)

if (INPUT):
    print('You said, ' + sys.stdin.readline().strip())
else:
    print('You said nothing!')

我正在使用此脚本测试select.select()并打印(str,end=“”)。我阅读了这篇文章()和这两个命令的官方Python3文档。

stdout
在默认情况下是缓冲的,要强制显示它,您需要刷新它:

print('Type > ', end='')
sys.stdout.flush()
请注意,
print
还通过关键字参数支持此功能:

print('Type > ', end='', flush=True)

谢谢这很有效。在七分钟过去之前,我不能记下你的答案。我将在浏览器上打开此选项卡,以便标记您的答案。@DevynCollierJohnson无需担心-我认为,这不是巧合,我用select语句获得了对答案的赞成票,然后出现了这个问题:)