if - else

J

Jeff Wagner

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.

This is what I am trying to accomplish:

def MasterNumberRoutine(SumOfNumbers):

#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(SumOfNumbers / 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:
MasterNumberRoutine()
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 MasterNumberRoutine()
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(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2

If SumOfNumbers = 10 Then
GoTo Check10
ElseIf SumOfNumbers > 9 Then
MasterNumberRoutine
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("MasterNumberRoutine")
Resume Exit_Next

End Sub

Thanks, Jeff
 
J

John Roth

Jeff Wagner said:
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
 
J

Jeff Wagner

SumOfNumbers, return(), and none of

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

Awesome, thanks a lot!
Jeff
 
D

Duncan Booth

This is what I am trying to accomplish:

def MasterNumberRoutine(SumOfNumbers):

#This routine determines if the input is a master number and,
if not, #adds the resultant
if SumOfNumbers == 11 or 22 or 33:

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==22 or SumOfNumbers==33:
return SumOfNumbers

Or, you might be happier with the snappier:

if SumOfNumbers in (11,22,33):
return SumOfNumbers
#go back to where you came from, don't continue ...

I2 = int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2
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(SumOfNumbers, 10)

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

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.
elif SumOfNumbers > 9:
MasterNumberRoutine()
else:
return SumOfNumbers
Recursion may look cool, but for many situations, it can be clearer to
rewrite the whole think as an iterative solution:

def MasterNumberSolution(valueToSum):
while valueToSum > 9:
if valueToSum in (11,22,33):
return valueToSum
quotient, remainder = divmod(valueToSum, 10)
valueToSum = quotient + remainder
return valueToSum
 
J

John J. Lee

[...]

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
 
J

Jay O'Connor

John said:
[...]

if SumOfNumbers = 10:
#If SumOfNumbers = 10, then set it's value to 1
SumOfNumbers = 1
[...]

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

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

if SumOfNumbers = 10:
SumOfNumbers = 1

What happens with that? :)
 
B

Bengt Richter

This is what I am trying to accomplish:

def MasterNumberRoutine(SumOfNumbers):

#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(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2

if SumOfNumbers = 10:
^^^^^^^^^^^^^^^... Why make this test? 10>9 and 10/10+0==1 if you just let it recurse.
Or am I missing something?
#If SumOfNumbers = 10, then set it's value to 1
^-- double = for equality test in python, BTW
SumOfNumbers = 1
# control will just go to end of routine from here and return None by default
elif SumOfNumbers > 9:
MasterNumberRoutine()
else:
^^^^^-- you probably don't want this condition. Just undent the next line.
return SumOfNumbers
I'm not sure, but wouldn't (untested)

def MasterNumberRoutine(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
 
A

Andrew Koenig

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

Jeff Wagner

[...]

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

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
 
J

Jeff Wagner

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

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
 
C

Christopher Koppler

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.

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

Jeff Wagner

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

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

John J. Lee

Jeff Wagner said:
Yikes, I didn't even notice the = instead of the == ... thanks.

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
 
G

Georgy Pruss

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

Alan Gauld

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

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
http://www.freenetpages.co.uk/hp/alan.gauld
 
M

Michael Geary

| if (10 == sum) {...}
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...

The problem with "if( CONST == variable )" is that it reads unnaturally,
creating a mental speed bump when someone reads your code. Admittedly it's a
fairly minor speed bump, and avoiding coding errors is a good thing.
However, this contrivance is unnecessary with modern C compilers, which
issue a warning if you code "if( variable = CONST )".

With Microsoft Visual C++, I always set my project options so that warnings
are errors. If I code "if( variable = CONST )", it won't compile. Thus, I'm
able to use the more natural "if( variable == CONST )" notation without fear
of error.

It's certainly pleasant that Python sidesteps this issue completely! :)

-Mike
 
J

Jay O'Connor

Michael said:
The problem with "if( CONST == variable )" is that it reads unnaturally,
creating a mental speed bump when someone reads your code. Admittedly it's a
fairly minor speed bump, and avoiding coding errors is a good thing.
However, this contrivance is unnecessary with modern C compilers, which
issue a warning if you code "if( variable = CONST )".

I was on a TCL project (ugh..one reason I don't use TKinter is simply
guilt by association...I learned to hate TCL on that project) where I
inherited code from a developer who did a lot of that "if 10==x" kinda
stuff. Basically we said the same thing; "it doesn't read naturally"

> It's certainly pleasant that Python sidesteps this issue completely! :)

True, but I'd still rather allow the assignment and catch the error by
not allowing boolean comparison of non-boolean values. This would break
a lot of other basic stuff in Python, though, such as using "if seq:" to
determine if a sequence is empty, but I've never been a big fan of that
shortcut (althuogh I use it myself). I'd rather the code have to be a
bit more explicit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top