您可以将其汇总成一个for ... else语句,如下所示: for i in range(3): if a == i: do_task [a]() else: do_default_task() 你忘记了休息声明。在此循环中,else套件将始终执行 。有点证明了熊人的意思,因为 - 其他的真的是 棘手。 - mvh Bj?rn 例如,如果你有一个(琐碎的)if ... elif ...其他像这样: > 如果a == 0: do_task_0() elif a == 1: do_task_1() elif a == 2: do_task_2() else: do_default_task() 您可以将它汇总成一个for ... else语句,如下所示: for i in range(3): 如果a == i: do_task [a]() 重要的破解在这里失踪... else: do_default_task() 或者这个代码将do_task_i *和* do_default_task()...... -tkc 3月4日,10日:上午55点,BJ?林德奎斯特 < bjou ... @ gmail.comwrote: 2008年3月4日星期二下午4:17,Carl Banks< pavlovevide ... @ gmail .comwrote: for ...: ... 筋疲力尽: ... 坏了: ... 含义是明确的。而其他则是似乎在那里意义不大。 所以我可能喜欢Python 3.x的类似内容(或删除 else)。 我不会因为它自身的优点而反对这个,但是有一个 背后的理由名称else。如果你认为for循环是一个 汇总如果... elif ... else语句(这种情况这是 合理的往往是相同的其他人会有用),然后 else子句在for循环中保持不变。 例如,如果你有一个(琐碎的)if ... elif ...其他像这样: 如果a == 0: do_task_0() elif a == 1: do_task_1() elif a == 2: do_task_2() else: do_default_task() 您可以将其汇总成for ... else语句,如下所示: for i in range(3): if a == i: do_task [a]() 否则: do_default_task() 你忘记了break语句。在此循环中,else套件将始终执行 。有点证明了熊人的观点,因为 - 其他的确非常尴尬。 啊哈,但无论有没有其他 条款都会出错...... Carl Banks So far in Python I''ve almost hated the ''else'' of the ''for'' loops:- I have problems to remember its meaning;- It gives me little problems when I later want to translate Pythoncode to other languages (and you always have to translate long-livedcode).- I have used it only once, so far. So so far I''d liked to see it removed from Python 3.0. But then this article: http://tratt.net/laurie/tech_article...guage_featureshas shown me that my problems with the ''else'' of the ''for'' mostly comefrom just its bad naming. The converge language is yet another veryPython-like language, and it uses a better naming (the word"exhausted" is long and has a complex spelling for non-Englishspeakers, so it''s not perfect): for ...:...exhausted:...broken:... The meaning is explicit. While "else" seems to mean little there.So I may like something similar for Python 3.x (or the removal of the"else"). Bye,bearophile 解决方案 On Tue, Mar 4, 2008 at 4:17 PM, Carl Banks <pa************@gmail.comwrote: for ...: ... exhausted: ... broken: ... > The meaning is explicit. While "else" seems to mean little there. So I may like something similar for Python 3.x (or the removal of the "else"). I would not be opposed to this on its own merits, but there is a rationale behind the name "else". If you consider a for loop to be a rolled-up if...elif...else statement (situations where this is reasonable tend to be the same ones were else would be useful), then the "else" clause would remain unchanged on the for loop. For instance, if you have a (trivial) if...elif...else like this: if a == 0: do_task_0() elif a == 1: do_task_1() elif a == 2: do_task_2() else: do_default_task() You could roll it up into a for...else statement like this: for i in range(3): if a == i: do_task[a]() else: do_default_task()You forgot the break statement. The else suite will always be executedin this loop. Kind of proves bearophiles point, for-else is reallytricky.-- mvh Bj?rnFor instance, if you have a (trivial) if...elif...else like this:>if a == 0: do_task_0()elif a == 1: do_task_1()elif a == 2: do_task_2()else: do_default_task()You could roll it up into a for...else statement like this:for i in range(3): if a == i: do_task[a]()important "break" missing here... else: do_default_task() or otherwise this code will do_task_i *and* do_default_task()... -tkcOn Mar 4, 10:55 am, "BJ?rn Lindqvist" <bjou...@gmail.comwrote:On Tue, Mar 4, 2008 at 4:17 PM, Carl Banks <pavlovevide...@gmail.comwrote: for ...: ... exhausted: ... broken: ... The meaning is explicit. While "else" seems to mean little there. So I may like something similar for Python 3.x (or the removal of the "else"). I would not be opposed to this on its own merits, but there is a rationale behind the name "else". If you consider a for loop to be a rolled-up if...elif...else statement (situations where this is reasonable tend to be the same ones were else would be useful), then the "else" clause would remain unchanged on the for loop. For instance, if you have a (trivial) if...elif...else like this: if a == 0: do_task_0() elif a == 1: do_task_1() elif a == 2: do_task_2() else: do_default_task() You could roll it up into a for...else statement like this: for i in range(3): if a == i: do_task[a]() else: do_default_task() You forgot the break statement. The else suite will always be executedin this loop. Kind of proves bearophiles point, for-else is reallytricky.Ah ha, but that would have been a mistake with or without the elseclause....Carl Banks 这篇关于换别的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 18:45