简体   繁体   English

Python 3.x 中是否有 while(input()) 循环

[英]Is there a while(input()) loop 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我正在尝试编写一个 python 解决方案来解决 Kattis 上需要一个 while(input()) 循环的多少位数字我有一个 C++ 解决方案使用

int n;
while( cin >> n ) {}

but I was wondering if there was a way to do this in python 3.x但我想知道是否有办法在 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.一旦没有更多的输入要读取, input()调用将抛出 EOFError 并退出循环。

你可以使用 for 循环而不是 while for in where var 是一个变量,iterable 是一个列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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