Section 8.2. else Statement


8.2. else Statement

Like other languages, Python features an else statement that can be paired with an if statement. The else statement identifies a block of code to be executed if the conditional expression of the if statement resolves to a false Boolean value. The syntax is what you expect:

if expression:     expr_true_suite else:     expr_false_suite


Now we have the obligatory usage example:

if passwd == user.passwd:     ret_str = "password accepted"     id = user.id     valid = True else:     ret_str = "invalid password entered... try again!"     valid = False


8.2.1. "Dangling else" Avoidance

Python's design of using indentation rather than braces for code block delimitation not only helps to enforce code correctness, but it even aids implicitly in avoiding potential problems in code that is syntactically correct. One of those such problems is the (in)famous "dangling else" problem, a semantic optical illusion.

We present some C code here to illustrate our example (which is also illuminated by K&R and other programming texts):

/* dangling-else in C */ if (balance > 0.00)     if (((balance - amt) > min_bal) && (atm_cashout() == 1))       printf("Here's your cash; please take all bills.\n"); else     printf("Your balance is zero or negative.\n");


The question is, which if does the else belong to? In the C language, the rule is that the else stays with the closest if. In our example above, although indented for the outer if statement, the else statement really belongs to the inner if statement because the C compiler ignores superfluous white space. As a result, if you have a positive balance but it is below the minimum, you will get the horrid (and erroneous) message that your balance is either zero or negative.

Although solving this problem may be easy due to the simplistic nature of the example, any larger sections of code embedded within this framework may be a hair-pulling experience to root out. Python puts up guardrails not necessarily to prevent you from driving off the cliff, but to steer you away from danger. The same example in Python will result in one of the following choices (one of which is correct):

if balance > 0.00:     if balance - amt > min_bal and atm_cashout():         print "Here's your cash; please take all bills." else:     print 'Your balance is zero or negative.'


or

if balance > 0.00:     if balance - amt > min_bal and atm_cashout():         print "Here's your cash; please take all bills."     else:         print 'Your balance is zero or negative.'


Python's use of indentation forces the proper alignment of code, giving the programmer the ability to make a conscious decision as to which if an else statement belongs to. By limiting your choices and thus reducing ambiguities, Python encourages you to develop correct code the first time. It is impossible to create a dangling else problem in Python. Also, since parentheses are not required, Python code is easier to read.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net