Hi All

``````#Convert C to F
def Tc_to_Tf ():
Tc = input ("What is the Celsius Temperature ?   " )
Tf = 9.0/5.0 * Tc + 32
print 'The temperature is ', Tf, ' Degrees Fahrenheit'
``````

I get NameError: name 'Tf' is not defined
I dont understand as Tf is defined as a formulae?
Thanks in anticipation
Graham
Ps, dont think I have these tags right, some help here too please

## All 7 Replies

This works on my machine

``````#! /usr/bin/python

#Convert C to F
def Tc_to_Tf():
Tc = input ("What is the Celsius Temperature ? " )
Tf = 9.0/5.0 * Tc + 32
print 'The temperature is ', Tf, ' Degrees Fahrenheit'

Tc_to_Tf()``````

Like gerard4143 says, you have to make the print statement part of the function, since Tf is local to the function. The other option would be to return Tf to make it available outside the function:

``````#Convert C to F
def Tc_to_Tf ():
Tc = input ("What is the Celsius Temperature ?   " )
Tf = 9.0/5.0 * Tc + 32
return Tf

Tf = Tc_to_Tf ()
print 'The temperature is ', Tf, ' Degrees Fahrenheit'``````

Please use the [code=python] and [/code] tag pair to enclose your python code.

Alternatively you can use placeholder (same Snee's example)

``````#Convert C to F
def Tc_to_Tf ():
Tc = input ("What is the Celsius Temperature ?   " )
Tf = 9.0/5.0 * Tc + 32
return Tf

Tf = Tc_to_Tf ()
#.2f instructs Python that placeholder holds float number with 2decimal places. try 3f, 4f etc and see how it does
print 'The temperature is %.2f Degrees Fahrenheit' %(Tf, )``````

Hi Guys
Thanks very much for your post.
I understand now that I have to make Tf part of the function and how to do so.
I dont understand your Tf=Tc_toTf () command, could you please explain how it works.

Many thanks
Graham

Hi Guys
Thanks very much for your post.
I understand now that I have to make Tf part of the function and how to do so.
I dont understand your Tf=Tc_toTf () command, could you please explain how it works.

Many thanks
Graham

It simply means that function Tc_toTf() returns a value that you want to put into variable TF.

``````def Tc_to_Tf ():
Tc = input ("What is the Celsius Temperature ?   " )
Tf = 9.0/5.0 * Tc + 32
return Tf``````

Run it an nothing happends it only return Tf
Let do something simple in idle.

``````IDLE 2.6.2
>>> Tf = 50
>>> print 'The temperature is %.2f Degrees Fahrenheit' % (Tf)
The temperature is 50.00 Degrees Fahrenheit
>>>``````

So the only diffrence is that we put function call i a variable. `Tf = Tc_to_Tf ()`

Hi Folks