8.2 else Statement
else声明

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
和其他语言一样,Python提供了else声明来与if声明搭配。如果if 声明的条件表达式的结果是布尔假,那么else声明后的代码将会执行。语法如你希望的那样:
if expression:
expr_true_suite
else:
expr_false_suite
现在我们有一个必须使用的例子:

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