Else clause on Python while statement

前端 未结 12 2164
悲&欢浪女
悲&欢浪女 2020-11-22 08:24

I\'ve noticed the following code is legal in Python. My question is why? Is there a specific reason?

n = 5
while n != 0:
    print n
    n -= 1
else:
    pri         


        
相关标签:
12条回答
  • 2020-11-22 09:09

    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 = 5
    while n > 0:
        print n
        n -= 1
        if n == 2:
            break
    if n == 0:
        print n
    

    Now instead of checking condition after while loop we can swap it with else and get rid of that check.

    n = 5
    while n > 0:
        print n
        n -= 1
        if n == 2:
            break
    else:  # read it as "no_break"
        print n
    

    I always read it as while/no_break to understand the code and that syntax makes much more sense to me.

    0 讨论(0)
  • 2020-11-22 09:13

    It is useful for social interaction.

    while (Date != "January 1st"):
        time.sleep(1)
    else:
        print("Happy new year!")
    
    0 讨论(0)
  • 2020-11-22 09:14

    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:
        EXPRESSIONS
    print 'ELSE'
    print 'The next statement'
    

    and

    while CONDITION:
        EXPRESSIONS
    else:
        print 'ELSE'
    print 'The next statement'
    

    Because the print 'ELSE' statement seems always executed in both cases (both when the while loop finished or not run).

    Then, it's only different when the statement print 'ELSE' will not be executed. It's when there is a breakinside the code block under while

    In [17]: i = 0
    
    In [18]: while i < 5:
        print i
        if i == 2:
            break
        i = i +1
    else:
        print 'ELSE'
    print 'The next statement'
       ....:
    0
    1
    2
    The next statement
    

    If differ to:

    In [19]: i = 0
    
    In [20]: while i < 5:
        print i
        if i == 2:
            break
        i = i +1
    print 'ELSE'
    print 'The next statement'
       ....:
    0
    1
    2
    ELSE
    The next statement
    

    return is not in this category, because it does the same effect for two above cases.

    exception raise also does not cause difference, because when it raises, where the next code will be executed is in exception handler (except block), the code in else clause or right after the while clause will not be executed.

    0 讨论(0)
  • 2020-11-22 09:14

    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 the code below with the same results...

    n = 5
    while n != 0:
        print n
        n -= 1
    print "what the..."
    
    0 讨论(0)
  • 2020-11-22 09:18

    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 exception. It works for not only while and for loops, but also try blocks.

    You typically find it in places where normally you would exit a loop early, and running off the end of the loop is an unexpected/unusual occasion. For example, if you're looping through a list looking for a value:

    for value in values:
        if value == 5:
            print "Found it!"
            break
    else:
        print "Nowhere to be found. :-("
    
    0 讨论(0)
  • 2020-11-22 09:19

    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 fetch the next 100 records of data. You want the loop to go on as long as the length of the data received is 100. If it is less, you need it to go one more times and then end it. There are many other situations where you have no control over the last iteration. Having the option to add an else in these cases makes everything much easier.

    0 讨论(0)
提交回复
热议问题