[Tutor] Defining functions
John Carmona
jeannot18 at hotmail.com
Thu Mar 24 23:52:06 CET 2005
Hi there,
I have written (well almost as I copied some lines from an existing example)
this little programme - part of an exercise.
#By J Carmona
#Programme that compute area for
#circle, square, rectangle
def area_rect():
length = input("Length: ")
width = input ("Width: ")
print "The area is: ",length*width
def area_circ():
radius = input("What is the radius?: ")
print "The area is approximately: ", 3.14159*(radius**2)
def area_squ():
side = input ("What is the length of one side?: ")
print "The area is: ", side*side
def print_options():
print "------------------------------"
print "Options:"
print "1. print options"
print "2. calculate circle area"
print "3. calculate square area"
print "4. calculate rectangle area"
print "5. quit the programme"
print "------------------------------"
choice = input("Choose an option: ")
if choice == 1:
print_options()
elif choice == 2:
area_circ()
elif choice == 3:
area_squ()
elif choice == 4:
area_rect()
elif choice == 5:
print_options()
#Call starting menu
print_options()
If I try to change the 1, 2, 3, 4 or 5 by a letter i.e. a, b, c, d, e the
programme stop functionning. I get an error message saying that
Traceback (most recent call last):
File "C:/Python24/Example/area_cir_squ_regt.py", line 39, in -toplevel-
print_options()
File "C:/Python24/Example/area_cir_squ_regt.py", line 27, in print_options
choice = input("Choose an option: ")
File "<string>", line 0, in -toplevel-
NameError: name 'c' is not defined
What am I missing? Thanks
JC
More information about the Tutor
mailing list