Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Small Program Help

Status
Not open for further replies.

exit12

Technical User
Aug 19, 2003
44
GB
Hi all

I'm struggling with a simple program I wrote to calculate perimeters of squares and rectangles. (yeah I know... but I'm just starting out with practising functions).
Once the user has entered an option and found out their perimeter, I can get the menu showing again, but when the select another option it just quits out of the program. My code is below. If someone could point me in the direction of allowing the user to be able to continue selecting options, right up until the actually press 3 to quit.

Thank You.

#MENU
def main_menu():
print"Please choose an option below"
print"------------------------------"
print"1. Calculate perimeter of a Rectangle."
print"2. Calculate perimeter of a Square."
print"3. Quit."
print

def perrec(x,y):
return x+y+x+y

#Function 2 is the perimeter of square
def persquare(x):
return x*4


main_menu()
number = input("Please choose a number from menu: ")

if number==1:
x=input("Please enter width of Rectangle: ")
y=input("Please enter height of Rectangle: ")
print"Answer is ", perrec (x,y)
main_menu()
number = input("Please choose a number from menu: ")


elif number==2:
x=input("Please enter width of Square: ")
print"Answer is ", persquare (x)
main_menu()
number = input("Please choose a number from menu: ")


elif number!=3:
print"This is an invalid option."
main_menu()
number = input("Please choose a number from menu: ")


else:
print"You have quit.
 
Put your logic in a loop. When you call main_menu() the program does not go back up to where main_menu() is defined, it's not a GoTo. Find a tutorial that talks about loop constructs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top