In Python, def is an executable statement. Your function is not
defined until the def statement is run by Python. If you move your
function definitions earlier in the code so that your functions are
defined before they're used, this code will run properly.
Josh R.
On Aug 14, 2008, at 4:08 PM, Joseph Bae 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?
Thanks!
Joe
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor