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

Emad Nawfal (عماد نوفل) emadnawfal at gmail.com
Fri Aug 15 01:29:57 CEST 2008

```On Thu, Aug 14, 2008 at 7:08 PM, Joseph Bae <joeturf at gmail.com> wrote:

> Hi all,
>
> 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?
>

You just need to define the functions before you use them
Arranging the order od your program makes it work. Like this
def convertToFahrenheit(t):
tF = (9.0/5.0) * (t + 32)
return tF

def convertToCelsius(t):
tC = (9.0/5.0) * (t - 32)
return tC
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)

>
>
> Thanks!
>
> Joe
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>

--
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"