【发布时间】:2016-01-28 04:54:34
【问题描述】:
我想知道是否有任何方法可以为多个级别的 if 语句设置一个 else 语句。
我会详细说明:
if <condition-1>:
if <condition-2>:
do stuff
elif <condition-3>:
do other stuff
else: #if either condition-1 or all nested conditions are not met
do some other thing
我知道这可以通过添加一个带有“做其他事情”的函数并使用嵌套的 else 和顶层 else 调用它来轻松解决,但我想知道是否有某种方法可以做到这一点看起来干净一点。
提前致谢,欢迎提出任何想法。
【问题讨论】:
-
这里有类似的问题:stackoverflow.com/questions/2069662/how-to-exit-an-if-clause; tldr;包裹在自己的功能中
标签: python if-statement nested nested-if