python中while(cin>> var)的等效项是什么? [英] What's the equivalent for while (cin >> var) in python?
本文介绍了python中while(cin>> var)的等效项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在在线竞赛中,如果未指定输入长度,并且无法通过程序直接读取输入文件,则可以在C ++中使用此代码:
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
}
python等价于什么?
What's the equivalent for python?
- 不使用任何与文件相关的功能,例如
open() write() ...
推荐答案
Python中没有直接等效的方法.但是您可以使用两个嵌套循环对其进行仿真:
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)
这篇关于python中while(cin>> var)的等效项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文