Quantcast
Channel: Else clause on Python while statement - Stack Overflow
Browsing all 14 articles
Browse latest View live

Answer by Robert Siemer for Else clause on Python while statement

thing = 'hay'while thing: if thing == 'needle': print('I found it!!') # wrap up for break break thing = haystack.next()else: print('I did not find it.') # wrap up for no-breakThe possibly unfortunately...

View Article



Answer by Rahul Negi for Else clause on Python while statement

Suppose you've to search an element x in a single linked list def search(self, x): position = 1 p =self.start while p is not None: if p.info == x: print(x, " is at position ", position) return True...

View Article

Answer by Mansour.M for Else clause on Python while statement

As far as I know the main reason for adding else to loops in any language is in cases when the iterator is not on in your control. Imagine the iterator is on a server and you just give it a signal to...

View Article

Answer by Saif for Else clause on Python while statement

The else clause is only executed when the while-condition becomes false.Here are some examples:Example 1: Initially the condition is false, so else-clause is executed.i = 99999999while i < 5:...

View Article

Answer by Iluvatar for Else clause on Python while statement

I know this is old question but... As Raymond Hettinger said, it should be called while/no_break instead of while/else.I find it easy to understeand if you look at this snippet.n = 5while n > 0:...

View Article


Answer by Leo Ufimtsev for Else clause on Python while statement

Else is executed if while loop did not break.I kinda like to think of it with a 'runner' metaphor. The "else" is like crossing the finish line, irrelevant of whether you started at the beginning or end...

View Article

Answer by HVNSweeting for Else clause on Python while statement

My answer will focus on WHEN we can use while/for-else.At the first glance, it seems there is no different when using while CONDITION: EXPRESSIONSprint 'ELSE'print 'The next statement'and while...

View Article

Answer by Mark for Else clause on Python while statement

Allow me to give an example on why to use this else-clause. But:my point is now better explained in Leo’s answerI use a for- instead of a while-loop, but else works similar (executes unless break was...

View Article


Answer by Jean Ferri for Else clause on Python while statement

The better use of 'while: else:' construction in Python should be if no loop is executed in 'while' then the 'else' statement is executed. The way it works today doesn't make sense because you can use...

View Article


Answer by John Kugelman for Else clause on Python while statement

The else clause is executed if you exit a block normally, by hitting the loop condition or falling off the bottom of a try block. It is not executed if you break or return out of a block, or raise an...

View Article

Answer by BoltClock for Else clause on Python while statement

The else: statement is executed when and only when the while loop no longer meets its condition (in your example, when n != 0 is false).So the output would be this:54321what the...

View Article

Answer by Mark Rushakoff for Else clause on Python while statement

The else-clause is executed when the while-condition evaluates to false.From the documentation:The while statement is used for repeated execution as long as an expression is true:while_stmt ::= "while"...

View Article

Answer by ars for Else clause on Python while statement

The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won't be executed. One way to think about it is as an if/else...

View Article


Else clause on Python while statement

I've noticed the following code is legal in Python. My question is why? Is there a specific reason?n = 5while n != 0: print n n -= 1else: print "what the..."Many beginners accidentally stumble on this...

View Article
Browsing all 14 articles
Browse latest View live




Latest Images