I am trying to write a python solution to How many digits on Kattis which requires a while(input()) loop I have a C++ solution using
int n;
while( cin >> n ) {}
but I was wondering if there was a way to do this in python 3.x
I am trying to write a python solution to How many digits on Kattis which requires a while(input()) loop I have a C++ solution using
int n;
while( cin >> n ) {}
but I was wondering if there was a way to do this in python 3.x
I usually code it like this:
try:
while True:
n = int(input())
...
except EOFError:
pass
Once there is no more input to read, the input()
call will throw an EOFError and exit the loop.
You can use a for loop instead of a while for in where var is a variable and iterable is a list