Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的多个IF语句_Python_Python 2.7_If Statement - Fatal编程技术网

python中的多个IF语句

python中的多个IF语句,python,python-2.7,if-statement,Python,Python 2.7,If Statement,我正在尝试打印特定单元格中的内容。在将内容提取到输出之前,我知道要检查的单元格。我正在为此使用多个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语句:

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
输出形式为:-提取的内容,单元格编号

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

我得到的输出为B5和A4…但不是为A5


另外,只有在A5、B5和A4中没有内容的情况下,我如何检查B4中的内容…

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

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

这样,如果第一个行计数子句失败,您只能在每个行计数子句中运行第二个
if
语句,而不是每次都运行。

首先,您不会以
break
结束Python代码块。Python在看到您已缩进时结束代码块,如下所示:

if condition: //or any other statement that needs a block
    //code goes here
//end of block
break
语句用于终止它能找到的最内层循环。如果在循环下运行该代码,
break
语句可能会产生一些严重的错误

无论如何,有一种更为传统的方法来测试多种条件下的东西。您当前没有
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
请记住,Python在这样的语句中找到一个表达式后,它将运行相应的代码块并忽略所有其他块


希望这有帮助

达里安·穆迪(Darian Moody)有一个很好的解决方案:

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

您可以在python文档中阅读更多关于它的内容,以及更多信息和示例


(我在这里也用这些信息回答了类似的问题-

请扩展您的代码,以包括有关
行数
的信息。还有,你的缩进有点不对劲。“单元格”是什么?我希望你的缩进在粘贴它时被弄乱了,即使它确实有效。你可以
打印“a{}”。格式化(行数)
,顺便说一下,压缩一些代码你对
break
的实际作用做过研究吗,或者深入了解
if
语句的更多细节?这几乎满足了我的要求…但如果A5中有内容,我不希望A4被读取…现在我得到A4和A5的输出…对此有什么解决办法?@safwan只需将第二个外部if语句设为elif:e.g.
elif lineCount==4:
elif的意思,如果之前的if或elif条件不正确(并且只有在不正确的情况下),则检查此条件。elif代表elseif。还有
else
,它涵盖了您未包含的任何条件。将if语句放入if语句中是一种嵌套形式(嵌套if语句)。
a = 1
b = 2
c = True

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

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