Python 若语句已满,如何阻止代码继续到特定部分?

Python 若语句已满,如何阻止代码继续到特定部分?,python,python-3.x,if-statement,Python,Python 3.x,If Statement,我有多个IF语句,我希望执行这些语句,但一旦满足了任何IF语句的逻辑,我希望代码跳过未包含在任何IF`语句中的特定部分 我该怎么做 当前如何设置代码: if 1 in df.index: if df.col1.isnull()[1] or (df.col1[1]==''): [rest of the code] if 2 in df.index: if df.col1.isnull()[2] or (df.col1[2]==''):

我有多个
IF
语句,我希望执行这些语句,但一旦满足了任何IF语句的逻辑,我希望代码跳过未包含在任何IF`语句中的特定部分

我该怎么做

当前如何设置代码:

 if 1 in df.index:
        if df.col1.isnull()[1] or (df.col1[1]==''):
           [rest of the code]

 if 2 in df.index:
        if df.col1.isnull()[2] or (df.col1[2]==''):
           [rest of the code]

 if 3 in df.index:
        if df.col1.isnull()[3] or (df.col1[3]==''):
           [rest of the code]


[code I want to skip once any of the IF statements have been fulfilled]

[code I need to run regardless]

对第一个if语句使用
if
,然后对其余语句使用
elif
,或者将每个
if
语句包装成
try
除了
statement

对第一个if语句使用
if
然后对其余语句使用
elif
或者将每个
if
语句包装成
尝试
除了
语句

之外,您可以使用if、elif构造进行逻辑构建,但是正如您所提到的,您希望跳过/运行代码的某个特定部分,而该部分不是任何if块的一部分,根据在任何if块内完成的某些情况,可以使用标志在if块内翻转其值

flag = True;
if condition1:
  flag = False

if flag:
  code to be skipped

您可以使用if、elif构造进行逻辑构建,但正如您所提到的,您希望跳过/运行不是任何if块一部分的代码的特定部分,这取决于在任何if块内实现的某些情况,然后您可以使用标志在if块内翻转其值

flag = True;
if condition1:
  flag = False

if flag:
  code to be skipped

请确保您发布了正确的缩进,如果第二个
位于第一个
,则它看起来像第二个
one@RomanPerekhrest好眼力谢谢确保你贴了一个合适的缩进,现在,如果第二个
在第一个
中,它看起来像第二个
one@RomanPerekhrest好的眼睛谢谢没有理由使用try,除非在这种特殊情况下没有理由使用try,除非在这种特殊情况下