嵌套和非嵌套 if 的 Else 语句
Else statement for nested and unnested if
我想知道是否有任何方法可以为多级 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
我知道这可以通过添加带有 "do some other thing" 的函数并使用嵌套 else 和顶层 else 调用它来轻松解决,但我想知道是否有某种方法可以这看起来有点干净。
在此先致谢,欢迎提出任何想法。
不,不是真的。这确实是 python 不希望你做的事情。它更喜欢保持可读性和清晰度而不是 "flashy" 技巧。您可以通过组合语句或创建 "flag" 变量来做到这一点。
例如,您可以这样做
if <condition-1> and <condition-2>:
# do stuff
elif <condition-1> and <condition-3>:
# do other stuff
else:
# do some other thing
或者,如果您出于某种原因不想继续重复条件 1(检查成本高,不继续重复更清楚,或者您只是不想继续输入),我们可以做到
triggered_condition = False
if <condition-1>:
if <condition-2>:
triggered_condition = True
# do stuff
elif <condition-3>:
triggered_condition = True
# do some other stuff
if not triggered_condition:
# do some other thing
如果在函数中使用它,我们甚至可以跳过标志
if <condition-1>:
if <condition-2>:
# do stuff and return
elif <condition-3>:
# do some other stuff and return
# do some other thing
# if we got here, we know no condition evaluated to true, as the return would have stopped execution
有几种方法不是特别 intuitive/readable ...但有效:
在这一篇中,我们利用了 for ... else ...
语法。任何成功的条件都应该发出 break
for _ in [1]:
if <condition>:
if <condition>:
# where ever we consider ourselves "successful", then break
<do stuff>
break
elif <condition>:
if <condition>:
<do stuff>
break
else:
# we only get here if nothing considered itself successful
另一种方法是使用 try ... else ...
,其中 "successful" 分支应引发异常。
这些都不是特别好,不推荐!
我想知道是否有任何方法可以为多级 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
我知道这可以通过添加带有 "do some other thing" 的函数并使用嵌套 else 和顶层 else 调用它来轻松解决,但我想知道是否有某种方法可以这看起来有点干净。
在此先致谢,欢迎提出任何想法。
不,不是真的。这确实是 python 不希望你做的事情。它更喜欢保持可读性和清晰度而不是 "flashy" 技巧。您可以通过组合语句或创建 "flag" 变量来做到这一点。
例如,您可以这样做
if <condition-1> and <condition-2>:
# do stuff
elif <condition-1> and <condition-3>:
# do other stuff
else:
# do some other thing
或者,如果您出于某种原因不想继续重复条件 1(检查成本高,不继续重复更清楚,或者您只是不想继续输入),我们可以做到
triggered_condition = False
if <condition-1>:
if <condition-2>:
triggered_condition = True
# do stuff
elif <condition-3>:
triggered_condition = True
# do some other stuff
if not triggered_condition:
# do some other thing
如果在函数中使用它,我们甚至可以跳过标志
if <condition-1>:
if <condition-2>:
# do stuff and return
elif <condition-3>:
# do some other stuff and return
# do some other thing
# if we got here, we know no condition evaluated to true, as the return would have stopped execution
有几种方法不是特别 intuitive/readable ...但有效:
在这一篇中,我们利用了 for ... else ...
语法。任何成功的条件都应该发出 break
for _ in [1]:
if <condition>:
if <condition>:
# where ever we consider ourselves "successful", then break
<do stuff>
break
elif <condition>:
if <condition>:
<do stuff>
break
else:
# we only get here if nothing considered itself successful
另一种方法是使用 try ... else ...
,其中 "successful" 分支应引发异常。
这些都不是特别好,不推荐!