if - else

Alan Gauld alan.gauld at btinternet.com
Sat Nov 29 00:54:23 CET 2003

On Fri, 28 Nov 2003 23:28:51 GMT, Jeff Wagner
<JWagner at hotmail.com> wrote:

> def masterNumber(SumOfNumbers):
>     while SumOfNumbers not in range(1, 10) + [11, 22, 33]:
>         I2 = SumOfNumbers / 10
>         F2 = SumOfNumbers - (I2 * 10)
>         SumOfNumbers = I2 + F2
>     return SumOfNumbers
> masterNumber(57)
> print SumOfNumbers
> The last two lines are so I can pass the function various numbers to test it out. When I try to
> print out the returned value, I am getting the following error:
> "SumOfNumbers" is not defined.
> I thought that the return statement was supposed to return it's value to the place where it was
> called?

It returns the *value* not the variable name. Thus SumOfNumbers
is known only inside the function. You need to do something like:

     mySum = masterNumber(57)
     print mySum

mySum stores the value returned from masterNumbers, the one
corresponding to the name SumOfNumbers inside the function.

Or more simply you could just do:

     print masterNumber(57)

where print displays the value returned by the function without
storing it anywahere first


Alan G.

Author of the Learn to Program website

More information about the Python-list mailing list