if - else

John Roth newsgroups at jhrothjr.com
Thu Nov 27 03:30:16 CET 2003


"Jeff Wagner" <JWagner at hotmail.com> wrote in message
news:oanasvs703tipmup24qgc5toggte8uh67n at 4ax.com...
> 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(SumOfNumbers / 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.

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(SumOfNumbers / 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(SumOfNumbers, 10)
    SumOfNumbers = tens + units

John Roth






More information about the Python-list mailing list