【发布时间】:2015-09-04 04:51:56
【问题描述】:
如果在 Python 3.4 中我想读取值 (这里是 int) 直到用户给出输入。 像这样 C 代码
while( scanf("%d", &no) )
{
printf("%d" , no);
}
我试过类似的东西:
inp = input()
while inp != '':
print(int(inp))
inp = input()
只要我从终端手动输入并以 enter 或换行符结束输入
,上面的 python 代码就可以工作但它抛出:EOFError: EOF when reading a line 当我从 linux 终端中的标准输入读取时使用:
python3.4 filename.py < input
如果 input 文件不包含尾随 换行符。
编辑:
目前正在使用此方法,等待其他方法。
import sys
for line in sys.stdin:
do_anything() #here reading input
# End here
else_whatever() #just passing here
【问题讨论】:
-
那么您是在读取用户输入还是从文件中读取?这是两个完全不同的操作。另外,抛出
EOFError的代码在哪里? -
我帖子的上述python部分抛出了那个错误
-
你说当你从文件中读取时它会抛出
EOFError。您的问题中没有从文件中读取的任何代码。 -
先生编辑后有意义吗? @双位炼金术士
标签: python python-3.x