What's the equivalent for while (cin >> var) i

2019-06-23 21:49发布

问题:

In online contests, when the length of input is not specified and reading the input file directly through the program is not possible, one can use this code in C++:

while (cin >> var)
{
    //do something with var
}

What's the equivalent for python?

  • Without using any file-related function such as open() write() ...

回答1:

There's no direct equivalent in Python. But you can simulate it with two nested loops:

for line in sys.stdin:
    for var in line.split():

If you need something other than a string you'll need to convert it in a separate step:

        var = int(var)


回答2:

This could be helpfull.

import sys

for line in sys.stdin:
    #Do stuff


回答3:

write the following code

while True: a=input() if(a==''): break else ..... .

in else part you write the code which wants to execute and if you want to use int in your code convert it in int and use that



标签: python c++ input