13
如何退出if子句
有哪些方法可以过早退出if子句? 有时候,我在编写代码时希望将一条break语句放在一个if子句中,只是要记住,这些语句只能用于循环。 让我们以以下代码为例: if some_condition: ... if condition_a: # do something # and then exit the outer if block ... if condition_b: # do something # and then exit the outer if block # more code here 我可以想到一种方法:假设退出情况发生在嵌套的if语句内,请将其余代码包装在一个大else块中。例: if some_condition: ... if condition_a: # do something # and then exit …
104
python
control-flow