if - else
Robert Brewer
fumanchu at amor.org
Wed Nov 26 22:13:13 EST 2003
I'm not really sure why either version would bother to recurse (if
SumOfNumbers > 9), and then set the "return value" to 1 regardless.
Unless you snipped some of the VB code? My advice is to avoid recursion
and use a while: loop instead, if you can. I'm guessing a bit here,
because I'm not sure I have the logic right given your description. But,
with a quick bit of Googling, I think I now understand "master numbers",
at least in your context, e.g.:
http://www.simonhuggins.com/courses/cbasics/course_notes/session13.htm#e
xercise_53
So you probably want:
def MasterNumber(SumOfNumbers):
while SumOfNumbers not in range(1, 10) + [11, 22, 33]:
I2 = SumOfNumbers / 10 # no need for int(), since the
division returns an int.
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2
return SumOfNumbers
Notice that range(1, 10) produces [1, 2, 3, 4, 5, 6, 7, 8, 9]. Not the
fastest method, but it works. Oh, and the case of 10 is already handled
by feeding it through the loop again, so I dropped that short-circuit.
HTH!
Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org
> -----Original Message-----
> From: Jeff Wagner [mailto:JWagner at hotmail.com]
> Sent: Wednesday, November 26, 2003 6:20 PM
> To: python-list at python.org
> Subject: 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(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
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list