[Tutor] Giant Calc runs, but need advice on how to make the menu repopup.
Ewald Ertl
ewald.ertl at hartter.com
Tue Jul 12 14:21:14 CEST 2005
Hi!
As allready shown in your request for the "The Password Program" some day's ago,
which has nearly the same problem as here.
Wrap your code into loop's:
why are you setting the select-Values directly in the functions defining the menu's?
Why not just returning the selected value
e.g.:
def temp_menu():
print "1) Convert degrees Kevins to degrees Celsius"
print "2) Contvert Fahrenheit to Celsius"
print "3) Convert Celsius to Fahrenheit"
print "4) Convert Celsius to Kelvins"
print "5) Convert Kelvins to Fahrenheit"
print "6) Convert Fahrenheit to Kelvins"
print "7) Main Menu"
return (input("Choice: "))
and make the assignment in the main-Part:
temp_option=temp_menu()
So the function does not need to know which option the input should be assigned and
it is also useable in other part's of the code.
Here's an extract of your calc-Part with while-Loops:
there is only one call to the menu-functions necessary and not in all
branches, because we return every time in the loop except, when the
return-Option is selected. There is simple a "break"-Statement to leave
the loop and go on outside the loop.
In my opinion, it would als be more readable, when you extract the calculcation-Code
somewhere out of the big loop. Create a function for calculation and so on and handle
in the functions the code, so it will become more "readable".
It would also be somewhat better, to have a unique Number for the "Main Menu"-Option and not
everytime a different number to select the "Main Menu".
option=1
while option != 5:
option=main_menu()
if option == 1:
cal_opt=1
while cal_opt != 8:
cal_opt=cal_menu()
if cal_opt == 1:
X = input("First number:" )
Y = input("Second number:" )
print X, "+", Y, "= ",X + Y
elif cal_opt == 2:
X = input("First number:" )
Y = input("Second number:" )
print X, "-", Y, "= ",X - Y
elif cal_opt == 3:
X = input("First number:" )
Y = input("Second number:" )
print X, "*", Y, "= ",X * Y
elif cal_opt == 4:
X = input("First number:" )
Y = input("Second number:" )
if Y == 0:
print "Division by zero ot allowed!"
Y = input("Second number:" )
else:
print X, "/", Y, "= ",X / Y
elif cal_opt == 5:
X = input("First number:" )
Y = input("Second number:" )
if Y == 0:
print "Division by zero ot allowed!"
Y = input("Second number:" )
else:
print X, "/", Y, "= ",X / Y," R ", X % Y
elif cal_opt == 6:
X = input("First number:" )
Y = input("Power:" )
print X, "**", Y, "= ",X**Y
elif cal_opt == 7:
X = input("Number to be squared:" )
print "The square root of", X, " = ",X**0.5
elif cal_opt == 8:
break;
else:
print "That's not an option. Try again."
if option == 2:
...
One additional step is to make an independent menu-Function
def menu( args):
count=xrange(len(args))
for mycount,arg in zip( count,args):
print "%d) %s" % ( mycount, arg)
print "99) Main Menu"
return( input("Choice: "))
and just calling with the arguments
main_menu=[ "Calculate","Shapes","Temperature","Formulas","Quit" ]
calc_menu=["Add","Subraction","Multiplication","Division w/o remainder",
"Division with remaider","Exponation","Square roots" ]
option=menu(main_menu)
...
calc_option=menu(calc_menu)
....
HTH Ewald
on Tue, 12 Jul 2005 02:11:47 -0600 "Nathan Pinno" <falcon3166 at hotmail.com> wrote :
---------------------------------------------------------------------------------------------
Nathan Pinno > It runs but now how do I make it go back to the previous menu (e.g. from Add to Calculate Menu?)
Nathan Pinno >
Nathan Pinno > Here is the latest screenshot:
Nathan Pinno >
Nathan Pinno > The Giant Calculator
Nathan Pinno >
Nathan Pinno > Copyright 2005 Written and debugged by Nathan Pinno
Nathan Pinno >
Nathan Pinno > OPTIONS MENU
Nathan Pinno > 1) Calculate
Nathan Pinno > 2) Shapes
Nathan Pinno > 3) Temperature
Nathan Pinno > 4) Formulas
Nathan Pinno > 5) Quit
Nathan Pinno > What option would you like:1
Nathan Pinno > CALCULATE MENU
Nathan Pinno > 1) Add
Nathan Pinno > 2) Subraction
Nathan Pinno > 3) Multiplication
Nathan Pinno > 4) Division w/o remainder
Nathan Pinno > 5) Division with remaider
Nathan Pinno > 6) Exponation
Nathan Pinno > 7) Square roots
Nathan Pinno > 8) Back to the previous menu.
Nathan Pinno > What option would you like:3
Nathan Pinno > First number:3
Nathan Pinno > Second number:2
Nathan Pinno > 3 * 2 = 6
Nathan Pinno > CALCULATE MENU
Nathan Pinno > 1) Add
Nathan Pinno > 2) Subraction
Nathan Pinno > 3) Multiplication
Nathan Pinno > 4) Division w/o remainder
Nathan Pinno > 5) Division with remaider
Nathan Pinno > 6) Exponation
Nathan Pinno > 7) Square roots
Nathan Pinno > 8) Back to the previous menu.
Nathan Pinno > What option would you like:7
Nathan Pinno >
Nathan Pinno >
Nathan Pinno > >>>
Nathan Pinno >
Nathan Pinno > and the current code:
Nathan Pinno >
Nathan Pinno > # This program is designed as a big calculator with functions.
Nathan Pinno >
Nathan Pinno > # This first bunch of code is for the various menus. I decided to divide the calculations into seperate sub-menus,
Nathan Pinno > #so that the main menus would not be that long.
Nathan Pinno > def main_menu():
Nathan Pinno > global option
Nathan Pinno > print "OPTIONS MENU"
Nathan Pinno > print "1) Calculate"
Nathan Pinno > print "2) Shapes"
Nathan Pinno > print "3) Temperature"
Nathan Pinno > print "4) Formulas"
Nathan Pinno > print "5) Quit"
Nathan Pinno > option = input("What option would you like:" )
Nathan Pinno >
Nathan Pinno > def cal_menu():
Nathan Pinno > global cal_opt
Nathan Pinno > print "CALCULATE MENU"
Nathan Pinno > print "1) Add"
Nathan Pinno > print "2) Subraction"
Nathan Pinno > print "3) Multiplication"
Nathan Pinno > print "4) Division w/o remainder"
Nathan Pinno > print "5) Division with remaider"
Nathan Pinno > print "6) Exponation"
Nathan Pinno > print "7) Square roots"
Nathan Pinno > print "8) Back to the previous menu."
Nathan Pinno > cal_opt = input("What option would you like:" )
Nathan Pinno >
Nathan Pinno > def shape_menu():
Nathan Pinno > global shape_opt
Nathan Pinno > print "SHAPES MENU"
Nathan Pinno > print "Important: The figure that is used for pi is 3.14."
Nathan Pinno > print "1) Squares"
Nathan Pinno > print "2) Circles"
Nathan Pinno > print "3) Rectangles"
Nathan Pinno > print "4) Triangles"
Nathan Pinno > print "5) Cones"
Nathan Pinno > print "6) Cylinders"
Nathan Pinno > print "7) Semicircles"
Nathan Pinno > print "8) Main menu"
Nathan Pinno > shape_opt = input("What option would you like?" )
Nathan Pinno >
Nathan Pinno > def temp_menu():
Nathan Pinno > global temp_opt
Nathan Pinno > print "1) Convert degrees Kevins to degrees Celsius"
Nathan Pinno > print "2) Contvert Fahrenheit to Celsius"
Nathan Pinno > print "3) Convert Celsius to Fahrenheit"
Nathan Pinno > print "4) Convert Celsius to Kelvins"
Nathan Pinno > print "5) Convert Kelvins to Fahrenheit"
Nathan Pinno > print "6) Convert Fahrenheit to Kelvins"
Nathan Pinno > print "7) Main Menu"
Nathan Pinno > temp_option = input("Choice: ")
Nathan Pinno >
Nathan Pinno > def formula_menu():
Nathan Pinno > global formula_opt
Nathan Pinno > print "1) Interest"
Nathan Pinno > print "2) Distance"
Nathan Pinno > print "3) Uniform motion"
Nathan Pinno > print "4) Momentum"
Nathan Pinno > print "5) Uniformly accelerated motion"
Nathan Pinno > print "6) Falling bodies"
Nathan Pinno > print "7) Weight"
Nathan Pinno > print "8) Main menu"
Nathan Pinno > fourmula_option = input("Choice: ")
Nathan Pinno >
Nathan Pinno > #Code for main part of program.
Nathan Pinno > print "The Giant Calculator"
Nathan Pinno > print
Nathan Pinno > print "Copyright 2005 Written and debugged by Nathan Pinno"
Nathan Pinno > print
Nathan Pinno > main_menu()
Nathan Pinno > if option == 1:
Nathan Pinno > cal_menu()
Nathan Pinno > if cal_opt == 1:
Nathan Pinno > X = input("First number:" )
Nathan Pinno > Y = input("Second number:" )
Nathan Pinno > print X, "+", Y, "= ",X + Y
Nathan Pinno > cal_menu()
Nathan Pinno > elif cal_opt == 2:
Nathan Pinno > X = input("First number:" )
Nathan Pinno > Y = input("Second number:" )
Nathan Pinno > print X, "-", Y, "= ",X - Y
Nathan Pinno > cal_menu()
Nathan Pinno > elif cal_opt == 3:
Nathan Pinno > X = input("First number:" )
Nathan Pinno > Y = input("Second number:" )
Nathan Pinno > print X, "*", Y, "= ",X * Y
Nathan Pinno > cal_menu()
Nathan Pinno > elif cal_opt == 4:
Nathan Pinno > X = input("First number:" )
Nathan Pinno > Y = input("Second number:" )
Nathan Pinno > if Y == 0:
Nathan Pinno > print "Division by zero ot allowed!"
Nathan Pinno > Y = input("Second number:" )
Nathan Pinno > else:
Nathan Pinno > print X, "/", Y, "= ",X / Y
Nathan Pinno > cal_menu()
Nathan Pinno > elif cal_opt == 5:
Nathan Pinno > X = input("First number:" )
Nathan Pinno > Y = input("Second number:" )
Nathan Pinno > if Y == 0:
Nathan Pinno > print "Division by zero ot allowed!"
Nathan Pinno > Y = input("Second number:" )
Nathan Pinno > else:
Nathan Pinno > print X, "/", Y, "= ",X / Y," R ", X % Y
Nathan Pinno > cal_menu()
Nathan Pinno > elif cal_opt == 6:
Nathan Pinno > X = input("First number:" )
Nathan Pinno > Y = input("Power:" )
Nathan Pinno > print X, "**", Y, "= ",X**Y
Nathan Pinno > cal_menu()
Nathan Pinno > elif cal_opt == 7:
Nathan Pinno > X = input("Number to be squared:" )
Nathan Pinno > print "The square root of", X, " = ",X**0.5
Nathan Pinno > cal_menu()
Nathan Pinno > elif cal_opt == 8:
Nathan Pinno > main_menu()
Nathan Pinno > else:
Nathan Pinno > print "That's not an option. Try again."
Nathan Pinno > cal_menu
Nathan Pinno > elif option == 2:
Nathan Pinno > shape_menu()
Nathan Pinno > if shape_opt == 1:
Nathan Pinno > print "1) Circumference"
Nathan Pinno > print "2) Area"
Nathan Pinno > print "3) Shapes Menu"
Nathan Pinno > op = input("Choice: ")
Nathan Pinno > if op == 1:
Nathan Pinno > side = input("Side: ")
Nathan Pinno > print "Circumference = ",4*side
Nathan Pinno > shape_menu()
Nathan Pinno > elif op == 2:
Nathan Pinno > side = input("Side: ")
Nathan Pinno > print "Area = ",side**2
Nathan Pinno > shape_menu()
Nathan Pinno > elif op == 3:
Nathan Pinno > shape_menu()
Nathan Pinno > else:
Nathan Pinno > print "That's not an option."
Nathan Pinno > op = input("Choice: ")
Nathan Pinno > elif shape_opt == 2:
Nathan Pinno > print "1) Circumference"
Nathan Pinno > print "2) Area"
Nathan Pinno > print "3) Shapes Menu"
Nathan Pinno > d = input("Choice: ")
Nathan Pinno > if d == 1:
Nathan Pinno > diameter = input("Diameter: ")
Nathan Pinno > print "The circumference of the circle is ",diameter*3.14
Nathan Pinno > shape_menu()
Nathan Pinno > elif d == 2:
Nathan Pinno > radius = input("Radius: ")
Nathan Pinno > print "The area of the circle is ",3.14*(radius**2)
Nathan Pinno > shape_menu()
Nathan Pinno > elif d == 3:
Nathan Pinno > shape_menu()
Nathan Pinno > else:
Nathan Pinno > print "That's not an option."
Nathan Pinno > d = input("Choice: ")
Nathan Pinno > elif shape_opt == 3:
Nathan Pinno > print "1) Area"
Nathan Pinno > print "2) Perimeter"
Nathan Pinno > print "3) Main menu"
Nathan Pinno > g = input("Choice: ")
Nathan Pinno > if g == 1:
Nathan Pinno > base = input("Base: ")
Nathan Pinno > altitude = input("Altitude: ")
Nathan Pinno > print "The area of the rectangle is: ",base*altitude
Nathan Pinno > shape_menu()
Nathan Pinno > elif g == 2:
Nathan Pinno > base = input("Base: ")
Nathan Pinno > altitude = input("Altiutude: ")
Nathan Pinno > print "The perimeter of the rectangle is: ",(2*base)+(2*altitude)
Nathan Pinno > shape_menu()
Nathan Pinno > elif g == 3:
Nathan Pinno > shape_menu()
Nathan Pinno > else:
Nathan Pinno > print "That's not an option."
Nathan Pinno > g = input("Choice: ")
Nathan Pinno > elif shape_opt == 4:
Nathan Pinno > print "1) Right Triangles - Pygathorean Theorum"
Nathan Pinno > print "2) Perimeter"
Nathan Pinno > print "3) Area"
Nathan Pinno > print "4) Shapes Menu"
Nathan Pinno > e = input("Choice: ")
Nathan Pinno > if e == 1:
Nathan Pinno > sidea = input("Side A: ")
Nathan Pinno > sideb = input("side B: ")
Nathan Pinno > print "The hypotenuse's length is: ",((sidea**2)+(sideb**2))**0.5
Nathan Pinno > shape_menu()
Nathan Pinno > elif e == 2:
Nathan Pinno > sidea = input("Side A: ")
Nathan Pinno > sideb = input("Side B: ")
Nathan Pinno > sidec = input("Side C: ")
Nathan Pinno > print "The Perimeter of the triangle is: ",sidea+sideb+sidec
Nathan Pinno > shape_menu()
Nathan Pinno > elif e == 3:
Nathan Pinno > base = input("Base: ")
Nathan Pinno > height = input("Height: ")
Nathan Pinno > print "The area of the triangle is: ",1/2*base*height
Nathan Pinno > shape_menu()
Nathan Pinno > elif e == 4:
Nathan Pinno > shape_menu()
Nathan Pinno > else:
Nathan Pinno > print "That's not an option! Try again."
Nathan Pinno > e = input("Choice: ")
Nathan Pinno > elif shape_opt == 5:
Nathan Pinno > print "The base and circumference of a cone can be found with the circle formulas, so they aren't found here."
Nathan Pinno > print "1) Lateral Area"
Nathan Pinno > print "2) Total Area"
Nathan Pinno > print "3) Volume"
Nathan Pinno > print "4) Shapes Menu"
Nathan Pinno > opt = input("Choice: ")
Nathan Pinno > if opt == 1:
Nathan Pinno > r = input("Radius: ")
Nathan Pinno > sl = input("Slant height: ")
Nathan Pinno > print "The Lateral Area is: ",1/2*(2*3.14*r)*sl
Nathan Pinno > shape_menu()
Nathan Pinno > elif opt == 2:
Nathan Pinno > r = input("Radius: ")
Nathan Pinno > sl = input("Slant height: ")
Nathan Pinno > print "The total area is: ",1/2*(2*3.14*r)*sl+(3.14*(r**2))
Nathan Pinno > shape_menu()
Nathan Pinno > elif opt == 3:
Nathan Pinno > height = input("Height: ")
Nathan Pinno > r = input("Radius: ")
Nathan Pinno > print "The volume is: ",1/4*(3.14*(r**2))*height
Nathan Pinno > shape_menu()
Nathan Pinno > elif opt == 4:
Nathan Pinno > shape_menu()
Nathan Pinno > else:
Nathan Pinno > print "That's not an option"
Nathan Pinno > opt = input("Choice: ")
Nathan Pinno > elif shape_opt == 6:
Nathan Pinno > print "1) Lateral Area"
Nathan Pinno > print "2) Total Area"
Nathan Pinno > print "3) Volume"
Nathan Pinno > print "4) Shape Menu"
Nathan Pinno > g = input("Choice: ")
Nathan Pinno > if g == 1:
Nathan Pinno > r = input("Radius: ")
Nathan Pinno > height = input("Height: ")
Nathan Pinno > print "The Lateral Area is: ",(2*3.14*r)*height
Nathan Pinno > shape_menu()
Nathan Pinno > elif g == 2:
Nathan Pinno > r = input("Radius: ")
Nathan Pinno > height = input("Height: ")
Nathan Pinno > print "The Total Area is: ",((2*3.14*r)*height)+(2*(3.14*(r**2)))
Nathan Pinno > shape_menu()
Nathan Pinno > elif g == 3:
Nathan Pinno > r = input("Radius: ")
Nathan Pinno > height = input("Height: ")
Nathan Pinno > print "The volume is: ",(3.14*(r**2))*height
Nathan Pinno > shape_menu()
Nathan Pinno > elif g == 4:
Nathan Pinno > shape_menu()
Nathan Pinno > else:
Nathan Pinno > print "That is not an option!"
Nathan Pinno > g = input("Choice: ")
Nathan Pinno > elif shape_opt == 7:
Nathan Pinno > print "1) Arc Length"
Nathan Pinno > print "2) Area"
Nathan Pinno > print "3) Shape Menu"
Nathan Pinno > h = input("Choice: ")
Nathan Pinno > if h == 1:
Nathan Pinno > diam = input("Diameter: ")
Nathan Pinno > print "The arc length is: ",(3.14*diam)/2
Nathan Pinno > shape_menu()
Nathan Pinno > elif h == 2:
Nathan Pinno > r = input("Radius: ")
Nathan Pinno > print "The area is: ",(3.14*(r**2))/2
Nathan Pinno > shape_menu()
Nathan Pinno > elif h == 3:
Nathan Pinno > shape_menu
Nathan Pinno > else:
Nathan Pinno > print "Sorry, incorrect entry. Please try again."
Nathan Pinno > h = input("Choice: ")
Nathan Pinno > elif shape_menu == 8:
Nathan Pinno > main_menu()
Nathan Pinno > else:
Nathan Pinno > print "Sorry, not an option."
Nathan Pinno > shape_menu()
Nathan Pinno > elif option == 3:
Nathan Pinno > temp_menu()
Nathan Pinno > if temp_option == 1:
Nathan Pinno > print "Convert degrees Kelvin to degrees Celsius."
Nathan Pinno > k = input("Degrees Kelvin: ")
Nathan Pinno > print k-273.16," degrees Celsius"
Nathan Pinno > temp_menu()
Nathan Pinno > elif temp_option == 2:
Nathan Pinno > print "Convert Fahrenheit to Celsius"
Nathan Pinno > f = input("Degrees Fahrenheit: ")
Nathan Pinno > print 5/9*(f-32)," degrees Celsius"
Nathan Pinno > temp_menu()
Nathan Pinno > elif temp_option == 3:
Nathan Pinno > print "Convert Celsius to Fahrenheit"
Nathan Pinno > c = input("Degrees Celsius: ")
Nathan Pinno > print (9/5*C)+32," degrees Fahrenheit"
Nathan Pinno > temp_menu()
Nathan Pinno > elif temp_option == 4:
Nathan Pinno > print "Convert degrees Celsius to degrees Kelvin"
Nathan Pinno > c = input("Degrees Celsius: ")
Nathan Pinno > print c+273.16," degrees Kelvin"
Nathan Pinno > temp_menu()
Nathan Pinno > elif temp_option == 5:
Nathan Pinno > print "Convert degrees Kelvin to Fahrenheit"
Nathan Pinno > k = input("Degrees Kelvin: ")
Nathan Pinno > print ((k-273.16)*9/5)+32," degrees Fahrenheit"
Nathan Pinno > temp_menu()
Nathan Pinno > elif temp_option == 6:
Nathan Pinno > print "Convert Fahrenheit to degrees Kelvin"
Nathan Pinno > f = input("Degrees Fahrenheit: ")
Nathan Pinno > print (5/9*(f-32))+273.16," degrees Kelvin"
Nathan Pinno > temp_menu()
Nathan Pinno > elif temp_option == 7:
Nathan Pinno > main_menu()
Nathan Pinno > else:
Nathan Pinno > print "That's not an option. Please try again"
Nathan Pinno > temp_menu()
Nathan Pinno > elif option == 4:
Nathan Pinno > formula_menu()
Nathan Pinno > if formula_option == 1:
Nathan Pinno > p = input("Principal: ")
Nathan Pinno > r = input("Rate as a decimal: ")
Nathan Pinno > t = input("Time in years: ")
Nathan Pinno > print "Interest: ",p*r*t
Nathan Pinno > formula_menu()
Nathan Pinno > elif formula_option == 2:
Nathan Pinno > r = input("Rate: ")
Nathan Pinno > t = input("Time: ")
Nathan Pinno > print "Distance: ",r*t
Nathan Pinno > formula_menu()
Nathan Pinno > elif formula_option == 3:
Nathan Pinno > v = input("Velocity: ")
Nathan Pinno > t = input("Time: ")
Nathan Pinno > print "Uniform motion: ",v*t
Nathan Pinno > formula_menu()
Nathan Pinno > elif formula_option == 4:
Nathan Pinno > m = input("Mass: ")
Nathan Pinno > v = input("Velocity: ")
Nathan Pinno > print "Momentum: ",m*v
Nathan Pinno > formula_menu()
Nathan Pinno > elif formula_option == 5:
Nathan Pinno > as = input("Acceleration speed: ")
Nathan Pinno > t = input("Time: ")
Nathan Pinno > print "Uniformly accelerated motion: ",1/2*as*t
Nathan Pinno > formula_menu()
Nathan Pinno > elif formula_option == 6:
Nathan Pinno > gravity = input("Constant acceleration due to gravity: ")
Nathan Pinno > t = input("Time: ")
Nathan Pinno > print "Distance covered by falling body: ",1/2*gravity*(t**2)
Nathan Pinno > formula_menu()
Nathan Pinno > elif formula_option == 7:
Nathan Pinno > m = input("Mass: ")
Nathan Pinno > gravity = input("Gravity: ")
Nathan Pinno > print "Weight: ",m*gravity
Nathan Pinno > formula_menu()
Nathan Pinno > elif formula_option == 8:
Nathan Pinno > main_menu()
Nathan Pinno > else:
Nathan Pinno > print "That is not an option."
Nathan Pinno > formula_menu()
Nathan Pinno > elif option == 5:
Nathan Pinno > print "Goodbye!"
Nathan Pinno > else:
Nathan Pinno > print "That is not an option. Please choose an option from the menu."
Nathan Pinno > main_menu()
Nathan Pinno > print
Nathan Pinno >
Nathan Pinno >
Nathan Pinno > So how do I do it?
Nathan Pinno >
Nathan Pinno > Nathan Pinno
Nathan Pinno > http://www.npinnowebsite.ca/
------------------- end ----------------------
--
Ing. Ewald Ertl HartterGruppe Phone : +43-3352-33085-558
trinomic Projektmanagement & Informationstechnik GmbH Fax : +43-3352-33085-600
Wiener Straße 41 mailto:ewald.ertl at trinomic.com
A-7400 Oberwart http://www.trinomic.com mailto:ewald.ertl at hartter.com
More information about the Tutor
mailing list