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.
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."
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.