[Tutor] Beginner problem: name 'convertToFahrenheit' is not defined

Tuxicomane vivien.moreau at neuf.fr
Fri Aug 15 01:30:54 CEST 2008


Joseph Bae <joeturf at gmail.com> writes:

> Hi all,

Hi ! (and sorry for my approximative English ... ;-)

> I'm new to Python (and programming in general) and need some help!
>
> Here is my code so far for a temperature conversion program
>(converts between Fahrenheit and Celsius):
>
> temp = input("Enter A Number : ")
> convertTo = raw_input("Convert To (F)ahrenheit or (C)elsius? : ")
>
> if convertTo == "F":
>     convertedTemp = convertToFahrenheit(temp)
>     print "%d Celsius = %d Fahrenheit" % (temp, convertedTemp)
> else:
>     convertedTemp = convertToCelsius(temp)
>     print "%d Fahrenheit = %d Celsius" % (temp, convertedTemp)
>
> def convertToFahrenheit(t):
>     tF = (9.0/5.0) * (t + 32)
>     return tF
>
> def convertToCelsius(t):
>     tC = (9.0/5.0) * (t - 32)
>     return tC
>
> It worked fine without having extra functions but once I put
> convertToFahrenheit and convertToCelsius in (just for practice
> really), this happened:
>
> Enter A Number : 50
> Convert to (F)ahrenheit or (C)elsius? : F
> Traceback (most recent call last):
>      File "TemperatureConverter.py", line 5, in <module>
>           convertedTemp = convertToFahrenheit(temp)
> NameError: name 'convertToFahrenheit' is not defined
>
> This is most likely a very simple error, but can someone please
> clarify for me why it's behaving this way?

Well, you have to define the function convertToFahrenheit *before* you use it !
Then it should work as you want ;-)

> Thanks!
>
> Joe

You're welcome (and apologizes for my bad and simple English :-)

-- 
Vivien Moreau.


More information about the Tutor mailing list