if - else

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Wagner

    if - else

    I am trying to rewrite an old program written in VB. I can't figure out how to do the following:

    if SumOfNumbers == 11 or SumOfNumbers == 22 or SumOfNumbers == 33:
    return
    else:
    I2 = int(SumOfNumber s / 10)
    F2 = SumOfNumbers - (I2 * 10)
    SumOfNumbers = I2 + F2

    What I want this thing to do is if the SumOfNumbers is equivalent to any of the above, I want it to
    stop and return from whence it came. In VB, I had used a GOTO statement which doesn't exist in
    Python. I can not, for the life of me, figure out how to replace the GOTO function I was using. I
    have tried many options, i.e., continue, break, return, return SumOfNumbers, return(), and none of
    them work.

    This is what I am trying to accomplish:

    def MasterNumberRou tine(SumOfNumbe rs):

    #This routine determines if the input is a master number and, if not,
    #adds the resultant
    if SumOfNumbers == 11 or 22 or 33:
    #go back to where you came from, don't continue ...

    I2 = int(SumOfNumber s / 10)
    F2 = SumOfNumbers - (I2 * 10)
    SumOfNumbers = I2 + F2

    if SumOfNumbers = 10:
    #If SumOfNumbers = 10, then set it's value to 1
    SumOfNumbers = 1
    elif SumOfNumbers > 9:
    MasterNumberRou tine()
    else:
    return SumOfNumbers

    This is the old routine from VB which worked ... I know, it's crappy code. It's the first program I
    wrote many years ago.

    Public Sub MasterNumberRou tine()
    On Error GoTo ErrorHandler

    'This routine determines if the input is a master number and, if not,
    'adds the resultant

    Dim I2 As Integer
    Dim F2 As Integer

    If SumOfNumbers = 11 Or SumOfNumbers = 22 Or SumOfNumbers = 33 Then
    GoTo Done
    End If

    I2 = Int(SumOfNumber s / 10)
    F2 = SumOfNumbers - (I2 * 10)
    SumOfNumbers = I2 + F2

    If SumOfNumbers = 10 Then
    GoTo Check10
    ElseIf SumOfNumbers > 9 Then
    MasterNumberRou tine
    Else
    GoTo Done
    End If

    Check10:
    'If SumOfNumbers = 10, then set it's value to 1
    SumOfNumbers = 1

    Done:
    Exit_Next:
    Exit Sub

    ErrorHandler:
    MsgBox "Error: " & Err.Number _
    & vbCrLf & Err.Description , _
    vbOKOnly, "Unexpected Error"
    Call ErrLogger("Mast erNumberRoutine ")
    Resume Exit_Next

    End Sub

    Thanks, Jeff
  • John Roth

    #2
    Re: if - else


    "Jeff Wagner" <JWagner@hotmai l.com> wrote in message
    news:oanasvs703 tipmup24qgc5tog gte8uh67n@4ax.c om...[color=blue]
    > I am trying to rewrite an old program written in VB. I can't figure out[/color]
    how to do the following:[color=blue]
    >
    > if SumOfNumbers == 11 or SumOfNumbers == 22 or SumOfNumbers == 33:
    > return
    > else:
    > I2 = int(SumOfNumber s / 10)
    > F2 = SumOfNumbers - (I2 * 10)
    > SumOfNumbers = I2 + F2
    >
    > What I want this thing to do is if the SumOfNumbers is equivalent to any[/color]
    of the above, I want it to[color=blue]
    > stop and return from whence it came. In VB, I had used a GOTO statement[/color]
    which doesn't exist in[color=blue]
    > Python. I can not, for the life of me, figure out how to replace the GOTO[/color]
    function I was using. I[color=blue]
    > have tried many options, i.e., continue, break, return, return[/color]
    SumOfNumbers, return(), and none of[color=blue]
    > them work.[/color]

    Throw an exception. That will give the calling routine
    the opportunity to then do something different.

    I'm not sure what the surrounding code is so I'm going
    to assume that it's inline.

    try:
    if SumOfNumbers == 11 or SumOfNumbers == 22 or SumOfNumbers == 33:
    raise SomeException
    else:
    I2 = int(SumOfNumber s / 10)
    F2 = SumOfNumbers - (I2 * 10)
    SumOfNumbers = I2 + F2
    except:
    #whatever you need to do here


    Also, please use spaces when indenting. Some newsreaders
    don't indent at all when you use tabs. I've reindented your
    example above for clarity.

    By the way, there are probably easier ways to deal with
    numerology...

    Try something like this: (untested)

    if SumOfNumbers not in (11, 22, 33):
    tens, units = divmod(SumOfNum bers, 10)
    SumOfNumbers = tens + units

    John Roth


    Comment

    • Jeff Wagner

      #3
      Re: if - else

      On Wed, 26 Nov 2003 21:30:16 -0500, "John Roth" <newsgroups@jhr othjr.com> wrotf:
      [color=blue]
      >
      >"Jeff Wagner" <JWagner@hotmai l.com> wrote in message
      >news:oanasvs70 3tipmup24qgc5to ggte8uh67n@4ax. com...[color=green]
      >> I am trying to rewrite an old program written in VB. I can't figure out[/color]
      >how to do the following:[color=green]
      >>
      >> if SumOfNumbers == 11 or SumOfNumbers == 22 or SumOfNumbers == 33:
      >> return
      >> else:
      >> I2 = int(SumOfNumber s / 10)
      >> F2 = SumOfNumbers - (I2 * 10)
      >> SumOfNumbers = I2 + F2
      >>
      >> What I want this thing to do is if the SumOfNumbers is equivalent to any[/color]
      >of the above, I want it to[color=green]
      >> stop and return from whence it came. In VB, I had used a GOTO statement[/color]
      >which doesn't exist in[color=green]
      >> Python. I can not, for the life of me, figure out how to replace the GOTO[/color]
      >function I was using. I[color=green]
      >> have tried many options, i.e., continue, break, return, return[/color]
      >SumOfNumbers , return(), and none of[color=green]
      >> them work.[/color]
      >
      >Throw an exception. That will give the calling routine
      >the opportunity to then do something different.
      >
      >I'm not sure what the surrounding code is so I'm going
      >to assume that it's inline.
      >
      >try:
      > if SumOfNumbers == 11 or SumOfNumbers == 22 or SumOfNumbers == 33:
      > raise SomeException
      > else:
      > I2 = int(SumOfNumber s / 10)
      > F2 = SumOfNumbers - (I2 * 10)
      > SumOfNumbers = I2 + F2
      >except:
      > #whatever you need to do here
      >
      >
      >Also, please use spaces when indenting. Some newsreaders
      >don't indent at all when you use tabs. I've reindented your
      >example above for clarity.
      >
      >By the way, there are probably easier ways to deal with
      >numerology.. .
      >
      >Try something like this: (untested)
      >
      >if SumOfNumbers not in (11, 22, 33):
      > tens, units = divmod(SumOfNum bers, 10)
      > SumOfNumbers = tens + units
      >
      >John Roth
      >[/color]

      Awesome, thanks a lot!
      Jeff

      Comment

      • Duncan Booth

        #4
        Re: if - else

        Jeff Wagner <JWagner@hotmai l.com> wrote in
        news:oanasvs703 tipmup24qgc5tog gte8uh67n@4ax.c om:
        [color=blue]
        > This is what I am trying to accomplish:
        >
        > def MasterNumberRou tine(SumOfNumbe rs):
        >
        > #This routine determines if the input is a master number and,
        > if not, #adds the resultant
        > if SumOfNumbers == 11 or 22 or 33:[/color]

        Strangely, none of the replies I have seen mentioned the obvious problem,
        which is that the line above tests the three conditions "SumOfNumbers== 11",
        "22", and "33". If any of these three is true the whole expression is true,
        and both 22 and 33 are values which are always true.

        You want to write something like:

        if SumOfNumbers == 11 or SumOfNumbers==2 2 or SumOfNumbers==3 3:
        return SumOfNumbers

        Or, you might be happier with the snappier:

        if SumOfNumbers in (11,22,33):
        return SumOfNumbers[color=blue]
        > #go back to where you came from, don't continue ...
        >
        > I2 = int(SumOfNumber s / 10)
        > F2 = SumOfNumbers - (I2 * 10)
        > SumOfNumbers = I2 + F2[/color]
        These lines are trying to do integer division by 10 and then extract the
        remainder. You could use something like:

        I2, F2 = SumOfNumbers//10, SumOfNumbers%10

        or, you could do both operations at the same time:

        I2, F2 = divmod(SumOfNum bers, 10)

        Those variable names I2 and F2 are exactly meaningful or descriptive
        either.
        [color=blue]
        >
        > if SumOfNumbers = 10:
        > #If SumOfNumbers = 10, then set it's value to 1
        > SumOfNumbers = 1[/color]

        It seems to me that this test is completely superfluous. If you removed it,
        and the result was 10, then the recursive call would convert it to 1
        anyway.
        [color=blue]
        > elif SumOfNumbers > 9:
        > MasterNumberRou tine()
        > else:
        > return SumOfNumbers
        >[/color]
        Recursion may look cool, but for many situations, it can be clearer to
        rewrite the whole think as an iterative solution:

        def MasterNumberSol ution(valueToSu m):
        while valueToSum > 9:
        if valueToSum in (11,22,33):
        return valueToSum
        quotient, remainder = divmod(valueToS um, 10)
        valueToSum = quotient + remainder
        return valueToSum


        --
        Duncan Booth duncan@rcp.co.u k
        int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
        "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

        Comment

        • John J. Lee

          #5
          Re: if - else

          Duncan Booth <duncan@NOSPAMr cp.co.uk> writes:
          [color=blue]
          > Jeff Wagner <JWagner@hotmai l.com> wrote in
          > news:oanasvs703 tipmup24qgc5tog gte8uh67n@4ax.c om:[/color]
          [...][color=blue][color=green]
          > > if SumOfNumbers = 10:
          > > #If SumOfNumbers = 10, then set it's value to 1
          > > SumOfNumbers = 1[/color][/color]
          [...]

          Classic example of a worse-than-useless comment: it just repeats the
          code (even experienced programmers frequently fall into this trap,
          though they're not usually quite so blatant about it ;-). The idea is
          to explain why you're doing something, not just to restate it (which
          creates maintenance work and can positively mislead people if it's out
          of sync with the code). Think about the code you're *not* commenting:
          it's usually the context that's not clear to readers, not the detailed
          logic of the commented code.


          John

          Comment

          • Jay O'Connor

            #6
            Re: if - else

            John J. Lee wrote:
            [color=blue]
            >Duncan Booth <duncan@NOSPAMr cp.co.uk> writes:
            >
            >
            >[color=green]
            >>Jeff Wagner <JWagner@hotmai l.com> wrote in
            >>news:oanasvs7 03tipmup24qgc5t oggte8uh67n@4ax .com:
            >>
            >>[/color]
            >[...]
            >
            >[color=green][color=darkred]
            >>> if SumOfNumbers = 10:
            >>> #If SumOfNumbers = 10, then set it's value to 1
            >>> SumOfNumbers = 1
            >>>
            >>>[/color][/color]
            >[...]
            >
            >Classic example of a worse-than-useless comment: it just repeats the
            >code (even experienced programmers frequently fall into this trap,
            >though they're not usually quite so blatant about it ;-).
            >[/color]

            True, also someone's being careless with their code.

            if SumOfNumbers = 10:
            SumOfNumbers = 1

            What happens with that? :)




            Comment

            • Bengt Richter

              #7
              Re: if - else

              On Thu, 27 Nov 2003 02:20:09 GMT, Jeff Wagner <JWagner@hotmai l.com> wrote:
              [...][color=blue]
              >This is what I am trying to accomplish:
              >
              >def MasterNumberRou tine(SumOfNumbe rs):
              >
              > #This routine determines if the input is a master number and, if not,
              > #adds the resultant
              > if SumOfNumbers == 11 or 22 or 33:
              > #go back to where you came from, don't continue ...
              >
              > I2 = int(SumOfNumber s / 10)
              > F2 = SumOfNumbers - (I2 * 10)
              > SumOfNumbers = I2 + F2
              >
              > if SumOfNumbers = 10:[/color]
              ^^^^^^^^^^^^^^^ ... Why make this test? 10>9 and 10/10+0==1 if you just let it recurse.
              Or am I missing something?[color=blue]
              > #If SumOfNumbers = 10, then set it's value to 1[/color]
              ^-- double = for equality test in python, BTW[color=blue]
              > SumOfNumbers = 1[/color]
              # control will just go to end of routine from here and return None by default[color=blue]
              > elif SumOfNumbers > 9:
              > MasterNumberRou tine()
              > else:[/color]
              ^^^^^-- you probably don't want this condition. Just undent the next line.[color=blue]
              > return SumOfNumbers
              >[/color]
              I'm not sure, but wouldn't (untested)

              def MasterNumberRou tine(n):
              while n>9 and n not in (11, 22, 33): n = sum(divmod(n, 10))
              return n

              do about what you're doing (and without recursion)? Or what does that do wrong?

              Regards,
              Bengt Richter

              Comment

              • Andrew Koenig

                #8
                Re: if - else

                > True, also someone's being careless with their code.[color=blue]
                >
                > if SumOfNumbers = 10:
                > SumOfNumbers = 1
                >
                > What happens with that? :)[/color]

                In Python? Easy -- it doesn't compile.


                Comment

                • Jeff Wagner

                  #9
                  Re: if - else

                  On 27 Nov 2003 17:57:00 +0000, jjl@pobox.com (John J. Lee) wrotf:
                  [color=blue]
                  >Duncan Booth <duncan@NOSPAMr cp.co.uk> writes:
                  >[color=green]
                  >> Jeff Wagner <JWagner@hotmai l.com> wrote in
                  >> news:oanasvs703 tipmup24qgc5tog gte8uh67n@4ax.c om:[/color]
                  >[...][color=green][color=darkred]
                  >> > if SumOfNumbers = 10:
                  >> > #If SumOfNumbers = 10, then set it's value to 1
                  >> > SumOfNumbers = 1[/color][/color]
                  >[...]
                  >
                  >Classic example of a worse-than-useless comment: it just repeats the
                  >code (even experienced programmers frequently fall into this trap,
                  >though they're not usually quite so blatant about it ;-). The idea is
                  >to explain why you're doing something, not just to restate it (which
                  >creates maintenance work and can positively mislead people if it's out
                  >of sync with the code). Think about the code you're *not* commenting:
                  >it's usually the context that's not clear to readers, not the detailed
                  >logic of the commented code.
                  >
                  >
                  >John[/color]

                  I know, I wrote this program about 10 years ago (or something like that). I was glad it worked at
                  all. Now that I look back over it, I'm suprised it did worked at all. And now, I'm rewriting it in
                  a "real" language using "real" code .... never to return to VB ;-) I don't think learning VB did me
                  any good but I'm determined to escape those roadblocks and do things right no matter how long it
                  takes.

                  I appreciate all the comments and help very much. It is quite enlightening. My goal was to learn
                  Python and use this old program as a project so I could put to use what I'm learning in a real
                  project rather than just practice examples. I have found it is quite helpful.

                  I think with my determination and all the help I get here, I just may turn out to be a decent
                  programmer.

                  Jeff

                  Comment

                  • Jeff Wagner

                    #10
                    Re: if - else

                    On Thu, 27 Nov 2003 20:33:05 GMT, "Andrew Koenig" <ark@acm.org> wrotf:
                    [color=blue][color=green]
                    >> True, also someone's being careless with their code.
                    >>
                    >> if SumOfNumbers = 10:
                    >> SumOfNumbers = 1
                    >>
                    >> What happens with that? :)[/color]
                    >
                    >In Python? Easy -- it doesn't compile.
                    >[/color]

                    Ok, why won't it compile? I thought the standard if statement went like this -

                    if Condition:
                    suite

                    so why won't the above work? I realize it's not the best way to accomplish what I want to do, I'm
                    just trying to learn why it is an incorrect if statement so I know for the future.

                    Jeff

                    Comment

                    • Christopher Koppler

                      #11
                      Re: if - else

                      On Thu, 27 Nov 2003 21:43:31 GMT, Jeff Wagner <JWagner@hotmai l.com>
                      wrote:
                      [color=blue]
                      >On Thu, 27 Nov 2003 20:33:05 GMT, "Andrew Koenig" <ark@acm.org> wrotf:
                      >[color=green][color=darkred]
                      >>> True, also someone's being careless with their code.
                      >>>
                      >>> if SumOfNumbers = 10:
                      >>> SumOfNumbers = 1
                      >>>
                      >>> What happens with that? :)[/color]
                      >>
                      >>In Python? Easy -- it doesn't compile.
                      >>[/color]
                      >
                      >Ok, why won't it compile? I thought the standard if statement went like this -
                      >
                      >if Condition:
                      > suite
                      >
                      >so why won't the above work? I realize it's not the best way to accomplish what I want to do, I'm
                      >just trying to learn why it is an incorrect if statement so I know for the future.[/color]

                      It does assignment (=) instead of comparison (==) in the condition,
                      which is illegal in Python.


                      --
                      Christopher

                      Comment

                      • Jeff Wagner

                        #12
                        Re: if - else

                        On Thu, 27 Nov 2003 22:28:29 GMT, Christopher Koppler <klapotec@chell o.at> wrotf:
                        [color=blue]
                        >On Thu, 27 Nov 2003 21:43:31 GMT, Jeff Wagner <JWagner@hotmai l.com>
                        >wrote:
                        >[color=green]
                        >>On Thu, 27 Nov 2003 20:33:05 GMT, "Andrew Koenig" <ark@acm.org> wrotf:
                        >>[color=darkred]
                        >>>> True, also someone's being careless with their code.
                        >>>>
                        >>>> if SumOfNumbers = 10:
                        >>>> SumOfNumbers = 1
                        >>>>
                        >>>> What happens with that? :)
                        >>>
                        >>>In Python? Easy -- it doesn't compile.
                        >>>[/color]
                        >>
                        >>Ok, why won't it compile? I thought the standard if statement went like this -
                        >>
                        >>if Condition:
                        >> suite
                        >>
                        >>so why won't the above work? I realize it's not the best way to accomplish what I want to do, I'm
                        >>just trying to learn why it is an incorrect if statement so I know for the future.[/color]
                        >
                        >It does assignment (=) instead of comparison (==) in the condition,
                        >which is illegal in Python.[/color]

                        Yikes, I didn't even notice the = instead of the == ... thanks.

                        Comment

                        • John J. Lee

                          #13
                          Re: if - else

                          Jeff Wagner <JWagner@hotmai l.com> writes:[color=blue]
                          > On Thu, 27 Nov 2003 22:28:29 GMT, Christopher Koppler <klapotec@chell o.at> wrotf:[/color]
                          [...][color=blue][color=green]
                          > >It does assignment (=) instead of comparison (==) in the condition,
                          > >which is illegal in Python.[/color]
                          >
                          > Yikes, I didn't even notice the = instead of the == ... thanks.[/color]

                          That's why it's illegal :-)

                          In C, this has caused endless bugs. It's the reason experienced C
                          coders write stuff like:

                          if (10 == sum) {...}

                          rather than

                          if (sum == 10) {...}

                          -- the first won't compile if you accidentally write

                          if (10 = sum) {...}

                          Unfortunately, that doesn't get you out of all the trouble this
                          causes.

                          What would be nice in Python, I think, is an assignment operator for
                          assignment expressions that can't be mistaken or mistyped for equality
                          comparison. One trivial but annoyingly real problem is, ASCII has no
                          decent arrow symbol -- and unicode saturation is a long way off.
                          Guido seems to have no interest in the idea, anyway.


                          John

                          Comment

                          • Georgy Pruss

                            #14
                            Re: if - else


                            "John J. Lee" <jjl@pobox.co m> wrote in message news:87smk9mmgj .fsf@pobox.com. ..
                            | <...>|
                            | In C, this has caused endless bugs. It's the reason experienced C
                            | coders write stuff like:
                            |
                            | if (10 == sum) {...}
                            |
                            | rather than
                            |
                            | if (sum == 10) {...}

                            Yes, there's such a style, and it's considered by experienced C
                            programmers to be as weird as if( (a<b) == TRUE )...

                            Georgy

                            |
                            | -- the first won't compile if you accidentally write
                            |
                            | if (10 = sum) {...}
                            |
                            | <...>
                            | John


                            Comment

                            • Alan Gauld

                              #15
                              Re: if - else

                              On Fri, 28 Nov 2003 12:35:45 GMT, "Georgy Pruss"
                              <see_signature_ _@hotmail.com> wrote:
                              [color=blue]
                              > | if (10 == sum) {...}
                              > |
                              > | rather than
                              > |
                              > | if (sum == 10) {...}
                              >
                              > Yes, there's such a style, and it's considered by experienced C
                              > programmers to be as weird as if( (a<b) == TRUE )...[/color]

                              Dunno about that, I've been using C since 1986 and every C
                              project I've worked on since 1991 (about half a dozen biggish
                              ones) has mandated the

                              if (CONST == variable)...

                              style of if statement check, and most of the recent books
                              recommend it. So an awful lot of experienced C programmers
                              use it day in day out - and I haven't heard any loud complaints.
                              Generally anything that saves bugs in C is "A Good Thing", its
                              like running lint - you just build it into the make rules so that
                              you never forget...

                              Alan G.




                              Author of the Learn to Program website

                              Comment

                              Working...