Hi guys, I am back from my hols.
Jacob S gave me this little exercise to do a little while ago.
1) Make a program to compute the areas of several figures. Make it display a menu on startup, ask for a choice, and then compute the area of the figure chosen by asking for dimensions.
2) Make another script similiar to #1 using volumes instead of areas
I have decided to merge the 2 scripts. First I should have a menu asking me if I want to compute areas or volumes. Then depending on the choice it should display the relevant menu. My first menu comes on but if I select "b" or "c" the script does not run. The error message points out that "print_options()" or "print_options_2()" are not defined. Could somebody point me into the right direction, thanks.
--------------------------------------------------------------------------------------------- #By J Carmona #Programme that compute volumes or surfaces ##First menu is for the calculation of area ##Second menu is for the calculation of volume
##First ask the user what he wants to do running = True
def area_rect():
length = input("Length: ")
width = input ("Width: ")
print "The area is: ",length*widthdef 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*sidedef area_tgle():
base = input ("What is the base of the triangle?: ")
heigth = input ("What is the heigth of the triangle?: ")
print "The area is: ",base*heigth/2def vol_sph():
radius = input("What is the radius?: ")
print "The volume is: ", (4*3.14159*radius**3)/3def vol_cube():
side = input("Side: ")
print "The volume is: ",side**3def vol_box():
width = input ("What is the width of the box?: ")
length = input ("What is the length of the box?: ")
depth = input ("What is the depth of the box?: ")
print "The volume is: ", width*length*depthdef vol_cone():
radius = input ("What is the radiux of the base of the cone?: ")
heigth = input ("What is the heigth of the cone?: ")
print "The volume is: ", (1/3)(3.144159*(radius**2))(heigth)
def task_options(): print "---------------------------------------" print "Options:" print "a. Print options: " print "b. Do you want to calculate areas?: " print "c. Do you want to calculate volumes?: " print "d. Quit the programme" print "---------------------------------------" choice = raw_input("Choose an option: ") if choice == 'a': print task_options() elif choice == 'b': print print_options() elif choice == 'c': print print_options_2() elif choice == 'd': running = False print task_options()
def print_options(): print "------------------------------" print "Options:" print "a. print options" print "b. calculate circle area" print "c. calculate square area" print "d. calculate rectangle area" print "e. calculate triangle area" print "f. quit the programme" print "------------------------------" choice = raw_input("Choose an option: ") if choice == 'a': print_options() elif choice == 'b': area_circ() elif choice == 'c': area_squ() elif choice == 'd': area_rect() elif choice == 'e': area_tgle() elif choice == 'f': print_options() #Call starting menu print_options()
def print_options_2():
print "------------------------------"
print "Options:"
print "a. print options"
print "b. calculate the volume of a sphere"
print "c. calculate the volume of a cube"
print "d. calculate the volume of a box"
print "e. calculate the volume of a cone"
print "f. quit the programme"
print "------------------------------"
choice = raw_input("Choose an option: ")
if choice == 'a':
print_options()
elif choice == 'b':
vol_sph()
elif choice == 'c':
vol_cube()
elif choice == 'd':
vol_box()
elif choice == 'e':
vol_cone()
elif choice == 'e':
print_options()
#Call starting menu
print_options()
-------------------------------------------------------------------------------------------------------------------------------JC
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
