Multiple IF statements in python

48,755

Solution 1

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

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

Solution 2

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

a = 1
b = 2
c = True

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

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

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

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

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

Solution 3

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:

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

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

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

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.

Hope this helps!

Share:
48,755
safwan
Author by

safwan

Updated on June 03, 2020

Comments

  • safwan
    safwan almost 4 years

    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

    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

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

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