简体   繁体   中英

how to take input in python when no of input are not defined

this is an c++ code , how can I implement this in python ?

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

This is roughly equivalent:

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

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

In Python, instead of using scanf and friends, usually you read a line or an entire file into a string, and use string operations to get the results you want.

In your example, you could do something like:

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

        ... = convert(number)

it can be done using try except

link below can be used

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM