[Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
Lie Ryan
lie.1296 at gmail.com
Sat Aug 16 13:50:35 CEST 2008
On Sat, 2008-08-16 at 07:40 +0200, tutor-request at python.org wrote:
> Message: 1
> Date: Fri, 15 Aug 2008 22:22:00 -0700
> From: "Joseph Bae" <joeturf at gmail.com>
> Subject: Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is
> not defined
> To: "Alan Gauld" <alan.gauld at btinternet.com>
> Cc: tutor at python.org
> Message-ID:
> <7bf8ebe60808152222v782a77bape757e46ef6ce5eba at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> 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:
>
You can fix some of these:
> def main():
> true = 1
> while true:
You can use True (note the capital (T)rue) for the boolean value of
True.
> try:
> temp = int(raw_input("Enter A Number : "))
> break
> except ValueError:
> print "Invalid Input"
Following the concept: Don't Make Me Repeat Myself, you could factor out
the input function:
def take_input(prompt,
validator = int,
errormessage = 'Invalid Input',
exceptions = (ValueError, )):
'''
prompt - String to be printed before raw_input
validator - Function to be called after raw_input
errormessage - String to be printed if any exceptions in
exceptions is raised
exceptions - List/Tuple containing list of exceptions to caught
'''
while True:
print prompt,
try:
return validator(raw_input())
except exceptions:
print errormessage
> while true:
> convertTo = raw_input("Convert To (F)ahrenheit or (C)elsius? :
> ")
> if not convertTo == "F" and not convertTo == "C":
If a boolean expression so simple looks complex, there is usually a
simpler version. Try learning boolean logic a little bit, specifically
boolean simplification for formal ways to simplify a boolean expression
(although many text about boolean logic subject is from electronic, the
basic rules are the same in programming and math).
> print "Invalid Input"
> * else:
> convertTo == "C" and convertToCelsius(temp) or
> convertToFahrenheit(temp)
Never use and-or trick except if you're writing for Python Obfuscation
Code Contest, they have huge pitfalls[1], the and-or trick was useful
before python got "inline if" (although Guido choose a frankly rather
unusual syntax):
true_value if expression else false_value
so your and-or trick should be:
convertToCelcius(temp) if convertTo == 'C' else
convertToFahrenheit(temp)
[1] in and-or hackery a problem arises for codes like this:
>>> b = 0
>>> c = 3
>>> True and b or c
3
>>> False and b or c
3
I'll leave it to you for brain exercise on why that thing happened
> break
> *
> def convertToCelsius(t):
> tC = (9.0/5.0) * (t - 32)
> print "%d Fahrenheit = %d Celsius" % (t, tC)
It's a bad idea for reusability to make convertToCelcius and
convertToFahrenheit print the result themselves, the function would be
more useful if it return the converted value and let the main program
print them. This way, the function could be used for other things as
well (for example, in a more complex temperature converter, rather than
typing out all to all converters, it's a much better idea to convert to
a common unit (say Kelvin, the SI standard for temperature), then
convert it again to the target unit. This is impossible to be done if
convertToCelcius prints the result themself.
i.e.:
def convertToCelcius(t):
return #Do some formula
t = 10
print '%s %s = %s %s' + (t, 'Fahrenheit', convertToCelcius(t),
'Celcius')
> def convertToFahrenheit(t):
> tF = (9.0/5.0) * (t + 32)
> print "%d Celsius = %d Fahrenheit" % (t, tF)
>
> if __name__=="__main__":
it's not a must, you should see PEP 8 (it's the style guideline):
http://www.python.org/dev/peps/pep-0008/
> 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
More information about the Tutor
mailing list