[Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
Steve Willoughby
steve at alchemy.com
Sat Aug 16 07:40:02 CEST 2008
Joseph Bae wrote:
> Thanks for the help!
>
> I have managed to get a good temperature converter program working! I am
> working on beefing it up a bit with some exception handling and an "and-or
> trick". The error handling works okay but I am having problems using and-or.
> Here's my updated code:
>
> def main():
> true = 1
> while true:
> try:
> temp = int(raw_input("Enter A Number : "))
> break
> except ValueError:
> print "Invalid Input"
> while true:
> convertTo = raw_input("Convert To (F)ahrenheit or (C)elsius? : ")
> if not convertTo == "F" and not convertTo == "C":
this might be easier to read as:
if convertTo != "F" and convertTo != "C":
or even:
if convertTo not in ("F", "C"):
> print "Invalid Input"
> * else:
> convertTo == "C" and convertToCelsius(temp) or
> convertToFahrenheit(temp)
I'd caution you against using and/or like this as a control flow
pattern. It can be very confusing compared to this:
if convertTo == "C":
convertToCelsius(temp)
else:
convertToFahrenheit(temp)
Note that the and/or pattern hides the bug you're facing. What you're
really doing with the and/or pattern is this:
if convertTo == "C":
if not convertToCelsuis(temp):
convertToFahrenheit(temp)
else:
convertToFahrenheit(temp)
So what you had will call convertToCelsuis(temp) AND if that didn't
return a true value, will ALSO call convertToFahrenheit(temp).
Since convertToCelsius() doesn't return a value at all, that's exactly
what happened.
> break
> *
> def convertToCelsius(t):
> tC = (9.0/5.0) * (t - 32)
> print "%d Fahrenheit = %d Celsius" % (t, tC)
>
> def convertToFahrenheit(t):
> tF = (9.0/5.0) * (t + 32)
> print "%d Celsius = %d Fahrenheit" % (t, tF)
That's not the correct calculation.
C = (F - 32) / 1.8
F = (C * 1.8) + 32
>
> if __name__=="__main__":
> main()
>
> Sample Output (as of right now):
>
> Enter A Number : 50
> Convert to (F)ahrenheit or (C)elsius? C
> 50 Fahrenheit = 32 Celsius
> 32 Celsius = 147 Fahrenheit <-- shouldn't show up and 147 is too high ...
>
> This only happens when I tell it to convert to C, if I say F it works
> normally. I've debugged it with pdb.set_trace() many times but can't figure
> out what's wrong. Help is much appreciated =)
>
> Thanks,
>
> Joe
>
> On Thu, Aug 14, 2008 at 10:50 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:
>
>> "Joseph Bae" <joeturf at gmail.com> wrote
>>
>> 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
>>>
>>> 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?
>>>
>> Others have explained that you need to execute the function
>> definitions before Python sees the name. You can do this in
>> two ways depending on taste.
>> 1) As suggested move your main code below the function definitions.
>> 2) move your main code into a function - traditionally called main()
>> then call main as the last line of your code.
>>
>> The second method has two advantages:
>> 1) It maintains the top-down design style if thats your preferred style
>> 2) It makes it much easier to make the file into a reusable module.
>>
>> It has two minor disadvantages:
>>
>> 1) The extra function call (main() ) slows things down by a tiny amount
>> 2) the extra indentation level of being inside a function reduces the page
>> width slightly
>>
>> HTH,
>>
>> --
>> Alan Gauld
>> Author of the Learn to Program web site
>> http://www.freenetpages.co.uk/hp/alan.gauld
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list