python中的多个IF语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41032495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:22:19  来源:igfitidea点击:

Multiple IF statements in python

pythonpython-2.7if-statement

提问by safwan

I am trying to print the content in a specific cell. i know the cells i want to check before extracting the content to the output. i am using multiple IF statements for this :

我正在尝试打印特定单元格中的内容。我知道在将内容提取到输出之前要检查的单元格。我为此使用了多个 IF 语句:

if lineCount == 5:
    if line[0]:
        print line[0], 'A5'
        OPfound = 1
        break
    if line[1]:
        print line[1], 'B5'
        OPfound = 1
        break
if lineCount == 4:
    if line[0]:
        print line[0], 'A4'
        OPfound = 1
        break
    if line[1]:
        print line[1],'B4'
        OPfound = 1
        break

The output is in the form :- extracted content, cell number

输出格式为:-提取的内容,单元格编号

what i am trying to do is first check if there is any content in A5 - if there is content then extract it...else check for content in B5 - if there is content then extract it...else check content in A4

我想要做的是首先检查 A5 中是否有任何内容 - 如果有内容则提取它...否则检查 B5 中的内容 - 如果有内容则提取它...否则检查 A4 中的内容

i am getting output for B5 and A4...but NOT FOR A5

我正在获得 B5 和 A4 的输出......但不是 A5

also how do i check content in B4 ONLY if there is no content in A5,B5 and A4...

如果 A5、B5 和 A4 中没有内容,我如何仅检查 B4 中的内容...

回答by burkesquires

Darian Moody has a nice solution to this challenge in his blog post:

Darian Moody 在他的博文中为这个挑战提供了一个很好的解决方案:

a = 1
b = 2
c = True

rules = [a == 1,
         b == 2,
         c == True]

if all(rules):
    print("Success!")

The all() method returns Truewhen all elements in the given iterable are true. If not, it returns False.

True当给定迭代中的所有元素都为真时,all() 方法返回。如果不是,则返回False

You can read a littlemore about it in the python docs hereand more information and examples here.

你可以阅读一点点在Python文档更多关于它在这里和更多的信息和例子在这里

(I also answered the similar question with this info here - How to have multiple conditions for one if statement in python)

(我也在这里用这个信息回答了类似的问题 - How to have multiple conditions for one if statement in python

回答by DrGodCarl

breakdoesn't let you leave ifclauses, if that's what you are indeed attempting to break out of. The trick here is to remove the breakstatements and replace your second ifs with elifs like so:

break不允许你离开if条款,如果那是你确实试图打破的。这里的技巧是删除break语句并将您的第二个ifs替换为elifs ,如下所示:

if lineCount == 5:
    if line[0]:
        print line[0],'A5'
        OPfound = 1
    elif line[1]:
        print line[1],'B5'
        OPfound = 1
if lineCount == 4:
    if line[0]:
        print line[0],'A4'
        OPfound = 1
    elif line[1]:
        print line[1],'B4'
        OPfound = 1

This way you are only running through the second ifstatement in each lineCount clause if the first one failed, not every time.

这样,if如果第一个语句失败,您只会运行每个 lineCount 子句中的第二个语句,而不是每次都运行。

回答by TheWildDefender

First off, you don't end a Python code block with break. Python ends a code block when it sees that you have indented back, like this:

首先,不要以break. 当 Python 看到您缩进时,它会结束一个代码块,如下所示:

if condition: //or any other statement that needs a block
    //code goes here
//end of block

The breakstatement is used to terminate the innermost loop it can find. If you're running that code under a loop, the breakstatement might produce some serious bugs.

break语句用于终止它可以找到的最内层循环。如果您在循环下运行该代码,该break语句可能会产生一些严重的错误。

Anyways, there is a much more conventional way of testing something for multiple conditions. Your current setup without the breakstatements should work, but I suggest you use an if...elif...elsestatement. Here's the format:

无论如何,有一种更传统的方法可以在多种条件下测试某些东西。您当前没有break语句的设置应该可以工作,但我建议您使用if...elif...else语句。这是格式:

if condition:
    //run if true
elif condition:
    //run if first expression was false, and this is true
elif condition:
    //run if second expression was false, and this is true

... (you get the idea)

else:
    //run if all other expressions are false

Keep in mind that after Python has found an expression that is true in such a statement, then it will run the corresponding block of code and ignore all other blocks.

请记住,当 Python 在这样的语句中找到一个为真的表达式后,它将运行相应的代码块并忽略所有其他块。

Hope this helps!

希望这可以帮助!