Python继续使用if或else

问题描述 投票:-2回答:2

在Python中:我想继续使用something2,如果有什么东西是真的,有些东西是真的,或者:

if something:
    if something3:
        number += 1
        {continue on something2}
else:
    if something2:
        if 2 == 2:
            number += 2

这可能吗?

python continue
2个回答
0
投票

我想我会这样写:

if something:
    if somethingElse:
        number += 1
        f()
else:
    f()

在两个案例中,f()被称为。只有当something是真的并且somethingElse不是真的时才会被调用f()


0
投票

无论如何,该计划将继续。

想象一下这样的功能。

def foo(condition)
    x = 0
    if condition:
       x = x + 1
    else:
       x = x + 2

    print x

无论何种条件,都将执行打印。程序不会因为if声明而停止。

if声明是简单的分支。您可以根据条件执行其中一个分支。如果if评估为true,您将进入第一个分支并在if语句后执行代码。如果条件是false,那么else之后的代码将被执行。

编辑:由于OP评论

如果某事和某事真实或虚假 - 继续

那你应该考虑一下你的逻辑。如果somethingtruesomething3不是,你不想继续。

if something:
  if something3:
     add_something()
  else:
     # do not continue. maybe return here

# continue with your code
© www.soinside.com 2019 - 2024. All rights reserved.