[英]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.