Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
On 8/16/08, Lie Ryan <[EMAIL PROTECTED]> wrote: > never use input(), use raw_input() instead. input() parses the string it > receives first, and may (read: WILL in the hands of certain persons) > allow user to input certain strings that get parsed into dangerous > codes. Use int(raw_input()) instead to convert the result of raw_input > (i.e. string) into a number (i.e. integer). Probably float(raw_input()) would be better in this program. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
On Sat, 2008-08-16 at 16:07 +, Matti/Tritlo wrote: > I too am a Beginner at python, and i have been playing around with it > for about a week. While playing around, i decided to make a calculator > program (A Very simple one) to calculate area and also to convert > farenheit to celcius and vice versa. So, here is the code: Shall I give some comments on your code? > def options(): > print "Options:" > print " 'p' to print options" > print " 's' for area of square" > print " 't' for area of triangle" > print " 'c' for area of square" > print " 'ce' to convert from celsius to farenheit" > print " 'fa' to convert from fahrenheit to celcius" > print " 'q' to quit the program" > print "Welcome to this calculator." > user = raw_input("So, What is your name? ") > print "Oh, Welcome ", user,",I´ll be your calculator to day." > print "Please select one of these options, and lets calculate!" > print options() > def positive(): > print "Must be a positive number" > > def square_area(width, height): > return width * height > > def triangle_area(width, height): > return width * height / 2 > > def circle_area (radius): > return radius * 3.141562953589793 > > def c_to_f(c_temp): > return 9.0 / 5.0 * c_temp + 32 > > def f_to_c(f_temp): > return (f_temp - 32.0) * 5.0 / 9.0 > > def choice(): > choice = "p" > while choice != "q": > choice = raw_input("Option: ") > if choice == "p": > print options() > elif choice == "s": > print "So, you have chosen to calculate the area of a > Square." > w = input("Please enter Width: ") never use input(), use raw_input() instead. input() parses the string it receives first, and may (read: WILL in the hands of certain persons) allow user to input certain strings that get parsed into dangerous codes. Use int(raw_input()) instead to convert the result of raw_input (i.e. string) into a number (i.e. integer). Since we're using int() to convert the string into number now, if you typed non-numbers, you'd get an Exception/Error, so you should add a try: block that would -- in case of errors -- reask the user again. try: int('string') except ValueError: print 'You passed a string that cannot be turned into integer' > while w <= 0: > positive() > w = input("Please enter Width: ") > h = input("Please enter Height: ") > while h <= 0: > positive() > h = input("Please enter Height: ") > print "The Square´s area is: ", square_area(w,h) > elif choice == "t": > print "So, you have chosen to calculate the area of a > Triangle." > w = input("Please enter Width: ") > while w <= 0: > positive() > w = input("Please enter Width: ") > h = input("Please enter Height: ") > while h <= 0: > positive() > h = input("Please enter Height: ") > print "The Triangle´s area is: ", triangle_area(w,h) Don't you think that all those codes seems very similar and redundant and tiring to type? Then make it into a function, pass arguments to make the behavior of the function differs and cut short a lot of lines. def getanumber(prompt): h = int(raw_input(prompt)) while h <= 0: positive() h = int(raw_input(prompt)) return h ... elif choice == 't': print "So, you have chosen to calculate the area of a Triangle." w = getanumber("Please enter Width: ") h = getanumber("Please enter Height: ") print "The Triangle´s area is: ", triangle_area(w,h) elif choice ... Ok, now we're left with that, which is still redundant since the pattern of print, get input values, and print output values is still repeated. Personally I'd go even further to make them functions too, but I'll let you pass with that (why? because: since not all menu options require the same number of argument it'd require using a clever trick involving list/dictionary and for-loop iteration, for now I think it might be out of bounds). PS: If you're really interested in how your program can still even be made more concise and less redundant, see way below for a raw, uncommented, !untested! code. > > elif choice == "c": > print "So, you have chosen to calculate the area of a > Circle." > r = input("Please enter radius: ") > while r <= 0: > positive () > r = input("Please enter radius: ") > print "The Circle´s area is: ", circle_area (r) > elif choice == "ce": > print "So, you´re wondering how Celcius relates to > Farenheit." > c = input("Please enter Celcius degree´s:
Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
On Sat, Aug 16, 2008 at 12:07 PM, Matti/Tritlo <[EMAIL PROTECTED]> wrote: Some quick notes: > print options() No need for the 'print' here, options() already prints. The extra print will print the return value of options(), which is None. > def triangle_area(width, height): > return width * height / 2 This will give an incorrect result if width and height are both odd integers. Use 2.0 instead of 2 to force float division. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
I too am a Beginner at python, and i have been playing around with it for about a week. While playing around, i decided to make a calculator program (A Very simple one) to calculate area and also to convert farenheit to celcius and vice versa. So, here is the code: def options(): print "Options:" print " 'p' to print options" print " 's' for area of square" print " 't' for area of triangle" print " 'c' for area of square" print " 'ce' to convert from celsius to farenheit" print " 'fa' to convert from fahrenheit to celcius" print " 'q' to quit the program" print "Welcome to this calculator." user = raw_input("So, What is your name? ") print "Oh, Welcome ", user,",I´ll be your calculator to day." print "Please select one of these options, and lets calculate!" print options() def positive(): print "Must be a positive number" def square_area(width, height): return width * height def triangle_area(width, height): return width * height / 2 def circle_area (radius): return radius * 3.141562953589793 def c_to_f(c_temp): return 9.0 / 5.0 * c_temp + 32 def f_to_c(f_temp): return (f_temp - 32.0) * 5.0 / 9.0 def choice(): choice = "p" while choice != "q": choice = raw_input("Option: ") if choice == "p": print options() elif choice == "s": print "So, you have chosen to calculate the area of a Square." w = input("Please enter Width: ") while w <= 0: positive() w = input("Please enter Width: ") h = input("Please enter Height: ") while h <= 0: positive() h = input("Please enter Height: ") print "The Square´s area is: ", square_area(w,h) elif choice == "t": print "So, you have chosen to calculate the area of a Triangle." w = input("Please enter Width: ") while w <= 0: positive() w = input("Please enter Width: ") h = input("Please enter Height: ") while h <= 0: positive() h = input("Please enter Height: ") print "The Triangle´s area is: ", triangle_area(w,h) elif choice == "c": print "So, you have chosen to calculate the area of a Circle." r = input("Please enter radius: ") while r <= 0: positive () r = input("Please enter radius: ") print "The Circle´s area is: ", circle_area (r) elif choice == "ce": print "So, you´re wondering how Celcius relates to Farenheit." c = input("Please enter Celcius degree´s: ") print c, "Degree´s Celcius are", c_to_f(c), "Degree´s Farenheit." elif choice == "fa": print "So, you´re wondering how Farenheit relates to Celcius." f = input("Please enter Farenheit degree´s: ") print f,"Degree´s Farenheit are", f_to_c(f), "Degree´s Celcius." else: print "That option does not exsist" choice = raw_input("Option: ") choice() print "Leaving eh, Well, Goodbye!" time.sleep(5) There you can see the Farenheit - Celcius part, and also the simple menu system. Hope this Helps! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
On Sat, 2008-08-16 at 07:40 +0200, [EMAIL PROTECTED] wrote: > Message: 1 > Date: Fri, 15 Aug 2008 22:22:00 -0700 > From: "Joseph Bae" <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is > not defined > To: "Alan Gauld" <[EMAIL PROTECTED]> > Cc: tutor@python.org > Message-ID: > <[EMAIL PROTECTED]> > 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
On Sat, Aug 16, 2008 at 1:22 AM, Joseph Bae <[EMAIL PROTECTED]> wrote: > convertTo == "C" and convertToCelsius(temp) or > convertToFahrenheit(temp) This idiom has a pitfall. Consider A and B or C If B can evaluate to a false value, such as None or 0, the expression will not do what you intend. This idiom is only safe when you are sure that B will never evaluate to false. In Python 2.5 there is a better way to write it: B if A else C This is a safer way to write your program. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
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 <[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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
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
Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
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
Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
"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
Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
Joseph Bae <[EMAIL PROTECTED]> writes: > Hi all, Hi ! (and sorry for my approximative English ... ;-) > 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 > 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? Well, you have to define the function convertToFahrenheit *before* you use it ! Then it should work as you want ;-) > Thanks! > > Joe You're welcome (and apologizes for my bad and simple English :-) -- Vivien Moreau. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
On Thu, Aug 14, 2008 at 7:08 PM, Joseph Bae <[EMAIL PROTECTED]> 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 > 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@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
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 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
[Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
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 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