Python 中的无限 for 循环
我是Python新手。实际上我用Java实现了一些东西,如下所示。
for(;;){
switch(expression){
case c1: statements
case c2: statements
default: statement
}
}
我如何在 Python 中实现这个?
I'm new to Python. Actually I implemented something using Java as shown below.
for(;;){
switch(expression){
case c1: statements
case c2: statements
default: statement
}
}
How do I implement this in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
或
You can use
or
使用 while 循环:
Use while loop:
正式来说,Python 中没有
switch
语句;它是一系列嵌套的if-elif-else
语句。无限循环由
while True
语句完成。全部在一起:
Formally, there's no
switch
statement in Python; it's a series of nestedif-elif-else
statements.Infinite loops are accomplished by the
while True
statement.All together:
如果您正在寻找一种在 python 中无限迭代的方法,您可以像 for 循环一样使用 itertools.count() 函数。 http://docs.python.org/py3k/library/itertools.html #itertools.count
If you are looking for a way to iterate infinitely in python you can use the itertools.count() function like a for loop. http://docs.python.org/py3k/library/itertools.html#itertools.count