if - else

Bengt Richter bokr at oz.net
Thu Nov 27 13:07:23 EST 2003


On Thu, 27 Nov 2003 02:20:09 GMT, Jeff Wagner <JWagner at 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




More information about the Python-list mailing list