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 16 1775
"Jeff Wagner" <JW*****@hotmail.com> wrote in message
news:oa********************************@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
On Wed, 26 Nov 2003 21:30:16 -0500, "John Roth" <ne********@jhrothjr.com> wrotf: "Jeff Wagner" <JW*****@hotmail.com> wrote in message news:oa********************************@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
Awesome, thanks a lot!
Jeff
Jeff Wagner <JW*****@hotmail.com> wrote in
news:oa********************************@4ax.com: 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
--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Duncan Booth <du****@NOSPAMrcp.co.uk> writes: Jeff Wagner <JW*****@hotmail.com> wrote in news:oa********************************@4ax.com:
[...] 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 ;-). 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
John J. Lee wrote: Duncan Booth <du****@NOSPAMrcp.co.uk> writes: Jeff Wagner <JW*****@hotmail.com> wrote in news:oa********************************@4ax.co m:
[...]
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? :)
On Thu, 27 Nov 2003 02:20:09 GMT, Jeff Wagner <JW*****@hotmail.com> wrote:
[...] 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
> 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.
On 27 Nov 2003 17:57:00 +0000, jj*@pobox.com (John J. Lee) wrotf: Duncan Booth <du****@NOSPAMrcp.co.uk> writes:
Jeff Wagner <JW*****@hotmail.com> wrote in news:oa********************************@4ax.com: [...] > 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 ;-). 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
On Thu, 27 Nov 2003 20:33:05 GMT, "Andrew Koenig" <ar*@acm.org> wrotf: 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.
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
On Thu, 27 Nov 2003 21:43:31 GMT, Jeff Wagner <JW*****@hotmail.com>
wrote: On Thu, 27 Nov 2003 20:33:05 GMT, "Andrew Koenig" <ar*@acm.org> wrotf:
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.
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.
--
Christopher
On Thu, 27 Nov 2003 22:28:29 GMT, Christopher Koppler <kl******@chello.at> wrotf: On Thu, 27 Nov 2003 21:43:31 GMT, Jeff Wagner <JW*****@hotmail.com> wrote:
On Thu, 27 Nov 2003 20:33:05 GMT, "Andrew Koenig" <ar*@acm.org> wrotf:
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.
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.
Yikes, I didn't even notice the = instead of the == ... thanks.
Jeff Wagner <JW*****@hotmail.com> writes: On Thu, 27 Nov 2003 22:28:29 GMT, Christopher Koppler <kl******@chello.at> wrotf:
[...] 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.
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
"John J. Lee" <jj*@pobox.com> wrote in message news:87************@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
On Fri, 28 Nov 2003 12:35:45 GMT, "Georgy Pruss"
<se*************@hotmail.com> wrote: | 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
> > | 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...
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
Michael Geary wrote: | 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...
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Diez B. Roggisch |
last post by:
Hi,
today I rummaged through the language spec to see whats in the for ... else:
for me. I was sort of disappointed to learn that the else clauses simply
gets executed after the loop-body -...
|
by: Christian Seberino |
last post by:
I am looking at the ELSE home page and trying to figure out if I should
invest the time to learn about the ELSE minor mode for Emacs.
Is there any programmer out there using ELSE that is getting...
|
by: Ron Adam |
last post by:
There seems to be a fair amount of discussion concerning flow control
enhancements lately. with, do and dowhile, case, etc... So here's my
flow control suggestion. ;-)
It occurred to me (a...
|
by: Patrice |
last post by:
Hi,
I need to do multi-conditional statements like below, but this error is
displayed :
Expected 'End'
/myFilepath, line x
else response.write(arrCorpo(sparam,sdiv)) end if
I don't...
|
by: WindAndWaves |
last post by:
Hi Team
The function below searches all the tables in a database. However, if
subsearch = true then it searches all the objects listed in a recordset
(which are all table names).
I thought to...
| |
by: Brie_Manakul |
last post by:
Is there a way in javascript to do an if else that shows a script in an
iframe? Let me know if that doesn't make sense. We have a portal and
in a portlet I need to grab these javascript links to...
|
by: Brie_Manakul |
last post by:
I need to set up an if else to show different weather scripts based on
the city selection they choose. Any help on this would be great.
Thanks!
<%@ page language="java" import="java.util.*,...
|
by: pelicanstuff |
last post by:
Hi - Was wondering if anybody could tell me why this rather crappy code is giving me an 'Else without If' error on compile?
All the Elses and Ifs look ok to me but there's a few.
Private Sub...
|
by: bearophileHUGS |
last post by:
So far in Python I've almost hated the 'else' of the 'for' loops:
- I have problems to remember its meaning;
- It gives me little problems when I later want to translate Python
code to other...
|
by: JRough |
last post by:
I'm trying to get error proof code.
I have this code which seems to work but now I look at it I think it
should be elseif not else and I wonder why it works. It is in the
block:...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |