Sorry! I copied the sample output from my command prompt incorrectly. Correct sample output:
Enter A Number : 50 Convert to (F)ahrenheit or (C)elsius? C 50 Fahrenheit = 32 Celsius *50 *(not 32) Celsius = 147 Fahrenheit Joe On Fri, Aug 15, 2008 at 10:22 PM, Joseph Bae <[EMAIL PROTECTED]> 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": > print "Invalid Input" > * else: > convertTo == "C" and convertToCelsius(temp) or > convertToFahrenheit(temp) > 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) > > 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 <[EMAIL PROTECTED]>wrote: > >> "Joseph Bae" <[EMAIL PROTECTED]> 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@python.org >> http://mail.python.org/mailman/listinfo/tutor >> > >
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor