[英]how to take input in python when no of input are not defined
这是一个c ++代码,如何在python中实现呢?
while(scanf("%lld",&n)!=EOF)
{
cout<<convert(n)<<endl;
}
这大致等效:
def convert(input):
#do something with your input
import sys
for line in sys.stdin:
convert(int(line))
在Python中,通常不使用scanf
和朋友,而是将一行或整个文件读取为字符串,然后使用字符串操作来获取所需的结果。
在您的示例中,您可以执行以下操作:
import sys
for line in sys.stdin:
for word in line.split():
number = int(word)
... = convert(number)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.