# [Tutor] using global variables with functions

Karl Pflästerer sigurd at 12move.de
Fri Nov 14 17:51:19 EST 2003

```On 14 Nov 2003, Smith, Ryan <- rsmith at cff.org wrote:

> #Prompt for user to enter the temp to convert
>>>>temp = raw_input("Enter a temperature:  ")
>>>>>print temp
>>>>>choice = raw_input("Convert to (F)arhenheit or (C)elcius: ")
> #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 convert_cel
> function.  My understanding is that whatever the user enters as the
> temperature that is now the value of the temp variable.  Since it is outside
> the function should it not be a global variable?  I get the following error
> message:

[same code]

> If anyone could help me see the "light" I would be most greatful.  I would
> like to point out that I am not asking for the answer just help with

We could speak about the design of your code but at the moment it should
just work.  Did you try at the Python prompt help(raw_input)?

If so you would have seen that the return value is always a string.  In
your function you must have a number not a string.

So how do you convert a string to a number?  If it is an integer you
write int(string) if it as a floating point value you write
float(string).

Then the second problem is your function.  Type at the python prompt 5/9
What is the answer?  I think 0.

That is because of the way Python treats integer division.  You can
either use a floating point number 5/9.0 or if your Python version is
newer than 2.2 you could write
from __future__ import division

Integer division will then behave like the floating point variant; if
you wanted the old behaviour you could write 5//9

If you never programmed before it will help you IMO if you read the
Python tutorial in the docs of Python.  There are also some very good
tutorials for beginners around especially for Python.  Take a look ath
the website http://www.python.org

And ask questions here.

Karl
--
Please do *not* send copies of replies to me.
I read the list

```