简体   繁体   English

复合如果elif else语句+ python

[英]compound if elif else statements + python

I am trying to pass an item out of the compound/nested if/elif/else statement. 我试图从复合/嵌套if / elif / else语句中传递一个项目。

if x == 0:
    do something
elif x == 1:
    do something else
elif x == 2:
    if x == 2 and x == whatever:
        do something
    elif x == 2 and x == whatever:
        do something
    else:
        pass it back out
elif x = 3:
    do something
else:
    do something

How can i pass it back out of the inner if so that it gets checked for whether its equal 3? 我怎样才能将它传回内部,如果它被检查是否等于3?

Does pass statement work here? 传递声明在这里工作吗? continue keeps throwing an error. 继续不断抛出错误。

if x == 0:
    do something
elif x == 1:
    do something else
elif x == 2 and y == whatever:
        do something
elif x == 2 and y == whatever:
        do something

elif x = 3:
    do something
else:
    do something

maybe? 也许? you cannot enter a new if/elif branch if you have already entered one 如果您已经输入了if / elif分支,则无法输入新的if / elif分支

another option is to move the rest inside 另一个选择是将其余部分移到里面

if x == 0:
    do something
elif x == 1:
    do something else
elif x >= 2:
    if x == 2 and x == whatever:
        do something
    elif x == 2 and x == whatever:
        do something
    elif x = 3:
        do something
    else:
        do something

the other option is to follow the other examples and start a new if block although you need to be a little careful not to check a condition that one of the other branches might change ... and also each option must be mutually exclusive of the other options 另一个选择是遵循其他示例并启动一个新的if块,尽管你需要小心不要检查其他分支之一可能会改变的条件......并且每个选项必须互相排斥另一个选项

You really don't need elif statements since you're checking the value of x in each one. 你真的不需要elif语句,因为你在每个语句中检查x的值。 Change them all to if statements, and change the final else statement to if x > 3: 将它们全部更改为if语句,并将最后的else语句更改为if x > 3:

You want to pass it out, but instead of an elif, use an if statement. 你想传递出来,但是使用if语句而不是elif。

else:
    pass it back out
if x  == 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM