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() ...
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)
This could be helpfull.
import sys
for line in sys.stdin:
#Do stuff
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