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

2019-06-23 21:57发布

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() ...

标签: python c++ input
3条回答
甜甜的少女心
2楼-- · 2019-06-23 22:18

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

查看更多
甜甜的少女心
3楼-- · 2019-06-23 22:31

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)
查看更多
疯言疯语
4楼-- · 2019-06-23 22:35

This could be helpfull.

import sys

for line in sys.stdin:
    #Do stuff
查看更多
登录 后发表回答