python中的多个IF语句 [英] Multiple IF statements in python

查看:91
本文介绍了python中的多个IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 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

我要做的是先检查A5中是否有任何内容-如果有内容,然后将其提取...否则检查B5中是否存在内容-如果有内容,则将其提取...其他检查内容在A4中

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

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

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

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

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

推荐答案

break 不允许您离开 if 子句,如果这确实是您要尝试破坏的子句在......之外.这里的技巧是删除 break 语句,并用 elif 替换第二个 if ,例如:

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

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

这样,如果第一个语句失败,则仅在每个lineCount子句中的第二个 if 语句中运行,而不是每次都如此.

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

这篇关于python中的多个IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆