6 Replies - 7815 Views - Last Post: 28 June 2010 - 05:53 PM Rate Topic: -----

#1 mat.roscoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 26-November 07

Nested if Statements

Posted 28 June 2010 - 04:35 PM

Hi Internet,

I am working on a project that requires multiple nested if statements where if any of the if statement conditions are broken i need to go to a single else condition. I will post an example of what I have done and I am hoping to either confirm that this methodology is correct or to critique what I have done or how I could improvement. You help is greatly appreciated!

if [Condition1] :               # This condition MUST be met!
    if [Condition2] :
        if [Condition3] :
            if [Condition4] :
                # So far into the code I would process the data
                # That has come through all the coniditons
    else:
        # Catching all data that breaks any if statements
        # that are above ^^/>



Is This A Good Question/Topic? 0
  • +

Replies To: Nested if Statements

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Nested if Statements

Posted 28 June 2010 - 05:04 PM

Hey there! What you are looking is the boolean operator and:

if [Condition1] and [Condition2] and [Condition3] and [Condition4]:
    do_everything()
else:
    print("One of the above conditions failed!")


Was This Post Helpful? 1
  • +
  • -

#3 mat.roscoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 26-November 07

Re: Nested if Statements

Posted 28 June 2010 - 05:15 PM

View PostMotoma, on 28 June 2010 - 04:04 PM, said:

Hey there! What you are looking is the boolean operator and:

if [Condition1] and [Condition2] and [Condition3] and [Condition4]:
    do_everything()
else:
    print("One of the above conditions failed!")




I tired this to no avail =( while it does make a lot of logical sense I am getting in the area of millions of results when i should be getting around a thousand I think I may have forgotten two for loops at the beginning...


for a in list1
    for b in list2
        if [Condition1] and [Condition2] and [Condition3] and [Condition4]:
            do_everything()
        else:
            print("One of the above conditions failed!")



do you think that this would work properly as i am trying to go through one list compare each object from the 1st list to an object in the second by attributes within the object.
Was This Post Helpful? 0
  • +
  • -

#4 mat.roscoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 26-November 07

Re: Nested if Statements

Posted 28 June 2010 - 05:26 PM

View PostMotoma, on 28 June 2010 - 04:04 PM, said:

Hey there! What you are looking is the boolean operator and:

if [Condition1] and [Condition2] and [Condition3] and [Condition4]:
    do_everything()
else:
    print("One of the above conditions failed!")




Also the 1st condition MUST pass so I am wondering how i would handle this...
Was This Post Helpful? 0
  • +
  • -

#5 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Nested if Statements

Posted 28 June 2010 - 05:31 PM

View Postmat.roscoe, on 28 June 2010 - 06:26 PM, said:

Also the 1st condition MUST pass so I am wondering how i would handle this...


Well, in the code I posted, all four conditions must be met.

So, I've shown you my code, it's time for you to show us yours.
Was This Post Helpful? 0
  • +
  • -

#6 mat.roscoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 26-November 07

Re: Nested if Statements

Posted 28 June 2010 - 05:33 PM

View Postmat.roscoe, on 28 June 2010 - 04:26 PM, said:

View PostMotoma, on 28 June 2010 - 04:04 PM, said:

Hey there! What you are looking is the boolean operator and:

if [Condition1] and [Condition2] and [Condition3] and [Condition4]:
    do_everything()
else:
    print("One of the above conditions failed!")




Also the 1st condition MUST pass so I am wondering how i would handle this...


Actually I managed to answer my own question! Your ands where the answer i just had to mainatin my structure somewhat:

for a in List1:
    for b in List2:
        if [Condition1] :
            if [Condition1] and\
               [Condition1] and\
               [Condition1] and\
               [Condition1] and\
               [Condition1]:
                
                # DO STUFF!
        else:
            # DO STUFF!




Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#7 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Nested if Statements

Posted 28 June 2010 - 05:53 PM

View Postmat.roscoe, on 28 June 2010 - 06:33 PM, said:

Actually I managed to answer my own question! Your ands where the answer i just had to mainatin my structure somewhat:
...
Thanks for your help!


Glad to hear you found a solution that works for you. If you have any other issues, don't hesitate to ask.

Cheers,
Motoma
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1