在python中没有定义任何输入时如何获取输入

2024-09-21 02:58:38 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个c++代码,如何在python中实现?你知道吗

while(scanf("%lld",&n)!=EOF)
    {
        cout<<convert(n)<<endl;
    }

Tags: 代码convertwhileeofcoutlldendlscanf
3条回答

在Python中,通常不使用scanf和friends,而是将一行或整个文件读入一个字符串,然后使用字符串操作来获得所需的结果。你知道吗

在您的示例中,您可以执行以下操作:

import sys
for line in sys.stdin:
    for word in line.split():
        number = int(word)

        ... = convert(number)

可以使用try-except

可以使用下面的链接

https://stackoverflow.com/a/38621838/4557946

这大致相当于:

def convert(input):
     #do something with your input

import sys
for line in sys.stdin:
    convert(int(line))

相关问题 更多 >

    热门问题