4

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

DrGodCarl
  • 1,666
  • 1
  • 12
  • 17
safwan
  • 87
  • 1
  • 1
  • 10

3 Answers3

15

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)

burkesquires
  • 1,345
  • 1
  • 14
  • 20
3

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.

DrGodCarl
  • 1,666
  • 1
  • 12
  • 17
  • this is giving me almost what i asked for...but if there is content in A5 i dont want A4 to be read...right now i am getting output for both A4 and A5...what will be the fix for this? – safwan Dec 08 '16 at 06:01
  • @safwan Just make the second outside if statement an elif: e.g. `elif lineCount==4:` – Brōtsyorfuzthrāx Dec 08 '16 at 06:22
  • elif means, if the previous if or elif condition wasn't true (and only if it wasn't) then check this one. elif stands for else if. There is also `else`, which covers any conditions you didn't include. Putting if statements within if statements is a form of nesting (nested if statements). – Brōtsyorfuzthrāx Dec 08 '16 at 06:32
2

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!