[Tutor] Beginner problem: name 'convertToFahrenheit' is not defined

Lie Ryan lie.1296 at gmail.com
Sat Aug 16 19:16:13 CEST 2008


On Sat, 2008-08-16 at 16:07 +0000, 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: ")
>                 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!
> 
> 

formulas = {
's': (
       'Square Area',
       ('Please enter Width: ', 'Please enter Height: '), 
       square_area,
     ),
't': (
       'Triangle Area',
       ('Please enter Width: ', 'Please enter Height: '), 
       triangle_area,
     ),
'c': ( 
       'Circle Area',
       ('Please enter Radius: ',),
       circle_area,
     ),
# etc
}

def getanumber(prompt):
    h = int(raw_input(prompt))
    while h <= 0:
        positive()
        h = int(raw_input(prompt))
    return h

def takeinputandcalculate(prompts, func):
    # got to choose a better, shorter name
    fargs = []
    for prompt in prompts:
        fargs.append(getanumber(prompt))
    return func(*fargs)

choice = raw_input('choose option [s, t, c]: ')
name, prompts, func = formulas[choice]
print 'So you have chosen to calculate %s' % name
result = takeinputandcalculate(prompts, func)
print 'That %s is %s' % (name, result)




More information about the Tutor mailing list