if - else
Dan Bishop
danb_83 at yahoo.com
Thu Nov 27 01:37:54 EST 2003
More information about the Python-list mailing list
Thu Nov 27 01:37:54 EST 2003
- Previous message (by thread): if - else
- Next message (by thread): if - else
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
> -----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 ...[attempt at rewriting an old VB program in Python]... > 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 Before translating that to Python, I'd try cleaning up the VB code. First, let's ignore the error-handling code (since it doesn't appear that you actually expect any errors. Public Sub MasterNumberRoutine() '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: End Sub The "Goto Check10" line can be eliminated by moving the Check10 block to each branch of the previous "If" that goes to Check10. Public Sub MasterNumberRoutine() '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 SumOfNumbers = 1 ElseIf SumOfNumbers > 9 Then MasterNumberRoutine SumOfNumbers = 1 ' Are you sure you wanted to do this? Else GoTo Done End If Done: End Sub The last "GoTo Done" is now redundant (because the next statement is at "Done" anyway). The first "GoTo Done" can also be eliminated by putting everything in an If block. Public Sub MasterNumberRoutine() 'This routine determines if the input is a master number and, if not, 'adds the resultant Dim I2 As Integer, F2 As Integer If not (SumOfNumbers = 11 Or SumOfNumbers = 22 Or SumOfNumbers = 33) Then I2 = Int(SumOfNumbers / 10) F2 = SumOfNumbers - (I2 * 10) SumOfNumbers = I2 + F2 If SumOfNumbers = 10 Then SumOfNumbers = 1 ElseIf SumOfNumbers > 9 Then MasterNumberRoutine SumOfNumbers = 1 ' Are you sure you wanted to do this? End If End If End Sub Notice that you now have no need to ask for Python's equivalent of "goto", because you didn't even need any goto's in VB! Furthermore, the "If SumOfNumbers = 10" branch is redundant, because if we rewrite the subroutine as Public Sub MasterNumberRoutine() 'This routine determines if the input is a master number and, if not, 'adds the resultant Dim I2 As Integer, F2 As Integer If not (SumOfNumbers = 11 Or SumOfNumbers = 22 Or SumOfNumbers = 33) Then I2 = Int(SumOfNumbers / 10) F2 = SumOfNumbers - (I2 * 10) SumOfNumbers = I2 + F2 If SumOfNumbers > 9 Then MasterNumberRoutine SumOfNumbers = 1 ' Are you sure you wanted to do this? End If End If End Sub then when SumOfNumbers is 10, the recursive call assigns I2 = 1 and F2 = 0, and then SumOfNumbers = 1, which is exactly what it did before. Translation to Python is now straightforward. def MasterNumberRoutine(): global SumOfNumbers if SumOfNumbers not in (11, 22, 33): I2, F2 = divmod(SumOfNumbers, 10) SumOfNumbers = I2 + F2 if SumOfNumbers > 9: MasterNumberRoutine() SumOfNumbers = 1 # Probably a bug. However, it would be better to make SumOfNumbers a parameter instead of a global variable.
- Previous message (by thread): if - else
- Next message (by thread): if - else
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list