[Tutor] Function not returning 05 as string [SOLVED]

Ken G. beachkidken at gmail.com
Mon Apr 13 15:10:55 CEST 2015


On 04/13/2015 08:56 AM, Steven D'Aprano wrote:
> On Mon, Apr 13, 2015 at 08:11:46AM -0400, Ken G. wrote:
>> I am sure there is an simple explanation but when I input
>> 5 (as integer), resulting in 05 (as string), I get zero as the end
>> result. When running the code:
>> number01 = 0
> Here you set the variable "number01" to the int 0.
>
>> numberentry()
> Here you call the function. It returns "05", but you don't do anything
> with it, so it just gets dropped on the floor and thrown away.
>
>> print
>> print number01
> Here you print the variable number01, which is still the int 0,
> unchanged.
>
> I think where you are mislead is that you are not aware, or have
> forgotten, that global variables and local variables are different. So a
> variable "x" outside of a function and a variable "x" inside a function
> are different variables. Think of functions being like Los Vegas: "what
> happens in Vegas, stays in Vegas" -- function local variables don't leak
> out and have effects outside of the function except via the "return"
> statement, and even then only if the caller assigns the result to
> another variable:
>
> function()  # return result is thrown away
> x = function()  # return result is assigned to x
>
>
> The other way to have an effect outside of the function is to use global
> variables, and the global keyword. This is occasionally necessary, but I
> strongly recommend against it: it is poor programming practice and a bad
> habit to get into.
>
>
Thanks for all the replies. I will keep at it. Again, thanks.

Ken


More information about the Tutor mailing list