Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97864 > unrolled thread

If one IF is satisfied, skip the rest in the nest...

Started bybigred04bd3@gmail.com
First post2015-10-21 10:31 -0700
Last post2015-10-22 02:08 +0000
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  If one IF is satisfied, skip the rest in the nest... bigred04bd3@gmail.com - 2015-10-21 10:31 -0700
    Re: If one IF is satisfied, skip the rest in the nest... Ian Kelly <ian.g.kelly@gmail.com> - 2015-10-21 11:39 -0600
    Re: If one IF is satisfied, skip the rest in the nest... John Gordon <gordon@panix.com> - 2015-10-21 17:46 +0000
    Re: If one IF is satisfied, skip the rest in the nest... bigred04bd3@gmail.com - 2015-10-21 10:55 -0700
    Re: If one IF is satisfied, skip the rest in the nest... Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-21 19:48 +0000
      Re: If one IF is satisfied, skip the rest in the nest... Grant Edwards <invalid@invalid.invalid> - 2015-10-21 20:07 +0000
        Re: If one IF is satisfied, skip the rest in the nest... Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-22 02:08 +0000

#97864 — If one IF is satisfied, skip the rest in the nest...

Frombigred04bd3@gmail.com
Date2015-10-21 10:31 -0700
SubjectIf one IF is satisfied, skip the rest in the nest...
Message-ID<50a6789a-3965-430b-9a91-b08adcedf7bf@googlegroups.com>
So here what I have, I have a 3 IF's within the same level.  If one IF is satisfied, I would like to "skip" the other IFs and continue with my code.

# 4 second open
            if wb1_sheet1.cell(row=cell + 1, column=2).value == 0 and wb1_sheet1.cell(row=cell + 1, column=3).value == 0 and wb1_sheet1.cell(row=cell + 2, column=2).value == 0 and wb1_sheet1.cell(row=cell + 2, column=3).value == 0 and wb1_sheet1.cell(row=cell + 3, column=2).value == 0 and wb1_sheet1.cell(row=cell + 3, column=3).value == 0:
                open += 3
                open_seconds += 4
                start = wb1_sheet1.cell(row=cell + 4, column=2).coordinate
                break
            # 3 second open
            if wb1_sheet1.cell(row=cell + 1, column=2).value == 0 and wb1_sheet1.cell(row=cell + 1, column=3).value == 0 and wb1_sheet1.cell(row=cell + 2, column=2).value == 0 and wb1_sheet1.cell(row=cell + 2, column=3).value == 0:
                open += 3
                start = wb1_sheet1.cell(row=cell + 3, column=2).coordinate
                open_seconds += 3
                continue
            # 2 second open
            if wb1_sheet1.cell(row=cell + 1, column=2).value == 0 and wb1_sheet1               .cell(row=cell + 1, column=3).value == 0:
                open += 3
                start = wb1_sheet1.cell(row=cell + 2, column=2).coordinate
                open_seconds += 2


if any but the last IF is true, then all IFs will be true...that's my problem.

any help would be great

[toc] | [next] | [standalone]


#97865

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-10-21 11:39 -0600
Message-ID<mailman.75.1445449220.878.python-list@python.org>
In reply to#97864
On Wed, Oct 21, 2015 at 11:31 AM,  <bigred04bd3@gmail.com> wrote:
> So here what I have, I have a 3 IF's within the same level.  If one IF is satisfied, I would like to "skip" the other IFs and continue with my code.

I think you're looking for the elif keyword. An elif branch will only
be considered if the previous branches were false.

if a:
    do_something()
elif b:
    do_something_else()
elif c:
    do_something_different()

[toc] | [prev] | [next] | [standalone]


#97866

FromJohn Gordon <gordon@panix.com>
Date2015-10-21 17:46 +0000
Message-ID<n08j1u$mo4$1@reader1.panix.com>
In reply to#97864
In <50a6789a-3965-430b-9a91-b08adcedf7bf@googlegroups.com> bigred04bd3@gmail.com writes:

> So here what I have, I have a 3 IF's within the same level.  If one IF is s=
> atisfied, I would like to "skip" the other IFs and continue with my code.

> # 4 second open
>             if wb1_sheet1.cell(row=cell + 1, column=2).value == 0 and wb1_sheet1.cell(row=cell + 1, column=3).value == 0 and wb1_sheet1.cell(row=cell + 2, column=2).value == 0 and wb1_sheet1.cell(row=cell + 2, column=3).value == 0 and wb1_sheet1.cell(row=cell + 3, column=2).value == 0 and wb1_sheet1.cell(row=cell + 3, column=3).value == 0:
>                 open += 3
>                 open_seconds += 4
>                 start = wb1_sheet1.cell(row=cell + 4, column=2).coord=
> inate
>                 break
>             # 3 second open
>             if wb1_sheet1.cell(row=cell + 1, column=2).value == 0 a=
> nd wb1_sheet1.cell(row=cell + 1, column=3).value == 0 and wb1_sheet=
> 1.cell(row=cell + 2, column=2).value == 0 and wb1_sheet1.cell(row=
> =cell + 2, column=3).value == 0:
>                 open += 3
>                 start = wb1_sheet1.cell(row=cell + 3, column=2).coord=
> inate
>                 open_seconds += 3
>                 continue
>             # 2 second open
>             if wb1_sheet1.cell(row=cell + 1, column=2).value == 0 a=
> nd wb1_sheet1               .cell(row=cell + 1, column=3).value == =
> 0:
>                 open += 3
>                 start = wb1_sheet1.cell(row=cell + 2, column=2).coord=
> inate
>                 open_seconds += 2


> if any but the last IF is true, then all IFs will be true...that's my probl=
> em.

It looks like all three of your if statements start out with this condition:

    if wb1_sheet1.cell(row=cell + 1, column=2).value == 0 and wb1_sheet1.cell(row=cell + 1, column=3).value == 0

So you could reorganize your code by putting an if statement at the top
that only checks this condition.  Then, indented underneath, you can
check for the other conditions.

And be sure to use "else" and "elif" when they are appropriate.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#97867

Frombigred04bd3@gmail.com
Date2015-10-21 10:55 -0700
Message-ID<da29bc79-93ad-4026-b0b0-5e38e3b0c248@googlegroups.com>
In reply to#97864
O...M....G I cannot believe I just asked this question ha.  I ended up realizing I need to use an elif after I took a break from it and re read everything...Brain fart, thanks for replies.

Brice

[toc] | [prev] | [next] | [standalone]


#97871

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-10-21 19:48 +0000
Message-ID<n08q5p$t7h$1@dont-email.me>
In reply to#97864
On Wed, 21 Oct 2015 10:31:04 -0700, bigred04bd3 wrote:

> So here what I have, I have a 3 IF's within the same level.  If one IF
> is satisfied, I would like to "skip" the other IFs and continue with my
> code.

c1 = wb1_sheet1.cell(row=cell + 1, column=2).value == 0 and 
     wb1_sheet1.cell(row=cell + 1, column=3).value == 0

c2 = wb1_sheet1.cell(row=cell + 2, column=2).value == 0 and 
     wb1_sheet1.cell(row=cell + 2, column=3).value == 0

c3 = wb1_sheet1.cell(row=cell + 3, column=2).value == 0 and 
     wb1_sheet1.cell(row=cell + 3, column=3).value == 0

if c1:
    if c2:
        if c3:
            # c1 && c2 && c3
            # 4 second open
        else:
            # c1 && c2
            # 3 second open
    else:
        # only c1
        # 2 second open

Each condition only gets evaluated once.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#97872

FromGrant Edwards <invalid@invalid.invalid>
Date2015-10-21 20:07 +0000
Message-ID<n08r9p$h8g$1@reader1.panix.com>
In reply to#97871
On 2015-10-21, Denis McMahon <denismfmcmahon@gmail.com> wrote:
> On Wed, 21 Oct 2015 10:31:04 -0700, bigred04bd3 wrote:
>
>> So here what I have, I have a 3 IF's within the same level.  If one IF
>> is satisfied, I would like to "skip" the other IFs and continue with my
>> code.
>
> c1 = wb1_sheet1.cell(row=cell + 1, column=2).value == 0 and 
>      wb1_sheet1.cell(row=cell + 1, column=3).value == 0
>
> c2 = wb1_sheet1.cell(row=cell + 2, column=2).value == 0 and 
>      wb1_sheet1.cell(row=cell + 2, column=3).value == 0
>
> c3 = wb1_sheet1.cell(row=cell + 3, column=2).value == 0 and 
>      wb1_sheet1.cell(row=cell + 3, column=3).value == 0
>
> if c1:
>     if c2:
>         if c3:
>             # c1 && c2 && c3
>             # 4 second open
>         else:
>             # c1 && c2
>             # 3 second open
>     else:
>         # only c1
>         # 2 second open

if c1 && c2 && c3:
    pass   # 4 seconds
elif c1 && c2:
    pass   # 3 seconds
elif c1:
    pass   # 2 seconds

Or if you want to be particulary obtuse:    

seconds = {0b111:4, 0b110:3, 0b100:2}.get(c1<<2 | c2<<1 | c3<<0, None)

> Each condition only gets evaluated once.

OK.

-- 
Grant Edwards               grant.b.edwards        Yow! You were s'posed
                                  at               to laugh!
                              gmail.com            

[toc] | [prev] | [next] | [standalone]


#97876

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-10-22 02:08 +0000
Message-ID<n09gef$t7h$3@dont-email.me>
In reply to#97872
On Wed, 21 Oct 2015 20:07:21 +0000, Grant Edwards wrote:

> On 2015-10-21, Denis McMahon <denismfmcmahon@gmail.com> wrote:
>> On Wed, 21 Oct 2015 10:31:04 -0700, bigred04bd3 wrote:
>>
>>> So here what I have, I have a 3 IF's within the same level.  If one IF
>>> is satisfied, I would like to "skip" the other IFs and continue with
>>> my code.
>>
>> c1 = wb1_sheet1.cell(row=cell + 1, column=2).value == 0 and
>>      wb1_sheet1.cell(row=cell + 1, column=3).value == 0
>>
>> c2 = wb1_sheet1.cell(row=cell + 2, column=2).value == 0 and
>>      wb1_sheet1.cell(row=cell + 2, column=3).value == 0
>>
>> c3 = wb1_sheet1.cell(row=cell + 3, column=2).value == 0 and
>>      wb1_sheet1.cell(row=cell + 3, column=3).value == 0
>>
>> if c1:
>>     if c2:
>>         if c3:
>>             # c1 && c2 && c3 # 4 second open
>>         else:
>>             # c1 && c2 # 3 second open
>>     else:
>>         # only c1 # 2 second open
> 
> if c1 && c2 && c3:
>     pass   # 4 seconds
> elif c1 && c2:
>     pass   # 3 seconds
> elif c1:
>     pass   # 2 seconds
> 
> Or if you want to be particulary obtuse:
> 
> seconds = {0b111:4, 0b110:3, 0b100:2}.get(c1<<2 | c2<<1 | c3<<0, None)

Not really valid, because #seconds n is simply a marker to indicate which 
branch of the OP's code to execute.

>> Each condition only gets evaluated once.
> 
> OK.

Yes, but in the structure I suggest, you can move the conditions back 
into the if statements and they still only each get evaluated once. Viz 
my alternative to the OP's code:

if wb1_sheet1.cell(row=cell + 1, column=2).value == 0 and wb1_sheet1.cell
(row=cell + 1, column=3).value == 0:

    if wb1_sheet1.cell(row=cell + 2, column=2).value == 0 and 
wb1_sheet1.cell(row=cell + 2, column=3).value == 0:

        if wb1_sheet1.cell(row=cell + 3, column=2).value == 0 and 
wb1_sheet1.cell(row=cell + 3, column=3).value == 0:

            open += 3 
            open_seconds += 4 
            start = wb1_sheet1.cell(row=cell + 4, column=2).coordinate

        else:

            open += 3 
            open_seconds += 3
            start = wb1_sheet1.cell(row=cell + 3, column=2).coordinate 

    else:

        open += 3 
        open_seconds += 2
        start = wb1_sheet1.cell(row=cell + 2, column=2).coordinate 

Not trying to be obtuse here, trying to suggest a practical solution.

Of course, the benefit of reducing the number of times each lookup into 
the worksheet is performed by reducing the number of times each 
comparison evaluated is going to depend on the computational and memory 
manipulation cost of doing so (I assume the workbook is loaded in memory, 
so no IO costs), and how frequently this set of comparisons is being 
performed.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web