[Tutor] Re: using global variables with functions

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Nov 16 19:14:20 EST 2003

> | #Function to convert from celcius to farenheit
> | >>>def convert_cel(temp):
> | ... tc = (5/9)*(temp - 32)
> | ... return tc
> | if choice == 'C':
> | print convert_cel(temp)
> |
> | My problem is when I try to use temp as a variable in the
> | function.  My understanding is that whatever the user enters as
> | temperature that is now the value of the temp variable.  Since it
is outside
> | the function should it not be a global variable?

The problem is that by caling the function parameter temp you
have created a new local variable in the function which hides
the global one.

The other problem as has been pointed out is that you are using
integer division, you need at least one value to be a float.
My tutorial deals with this issue in the very first coding topic,
the one on simple sequences.

global names are covered in the Advanced topic "What's in a Name?"

Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list