how do I loop all the way back the beginning

74 views Asked by At
print("================")
print("Area Calculator")
print("================")

base = 0
height = 0
length = 0
width = 0
area = 0
pie = 3.14
shape = 0

print("1) Triangle\n2) Rectangle\n3) Sqaure\n4) Circle\n5) Quit")

shape = int(input("Please type the number of the shape you would like to know the area of      here: "))

while shape > 4 or shape < 1:
  print("Inccorect input")
  shape = int(input("Please type the number of the shape you would like to know the area of    here: "))
else:
  print("continue")

if (shape == 1): 
  base = int(input("base:"))
  height = int(input("Height:"))
  area = base * height / 2
  print(area)
elif (shape == 2): 
  length = int(input("Length:"))
  width = int(input("Width:"))
  area = length * width
  print(area)
elif (shape == 3):
  side = int(input("Side:"))
  area = side **2
  print(area)
elif (shape == 4):
  radius = int(input("Radius:"))
  area = pie * radius **2
  print(area)

else:
  print("Something went wrong")

I want the user to be able to find the area of another shape after their first.

I know I can do something like this at the end

if not input("play again").lower() == "y":

but not sure how to start it, having a loop inside another loop through me off. Thanks for the help!

2

There are 2 answers

2
Chris On

Quite simply by wrapping the whole thing is a loop and breaking out of the loop when the input is 5 for "Quit".

Of course, you probably want to break some of this up into smaller functions.

while True:
  print("================")
  print("Area Calculator")
  print("================")
  
  base = 0
  height = 0
  length = 0
  width = 0
  area = 0
  pie = 3.14
  shape = 0
  
  print("1) Triangle\n2) Rectangle\n3) Sqaure\n4) Circle\n5) Quit")
  
  shape = int(input("Please type the number of the shape you would like to know the area of      here: "))
  
  while shape > 5 or shape < 1:
    print("Incorrect input")
    shape = int(input("Please type the number of the shape you would like to know the area of here: "))
  else:
    print("continue")

  if shape == 1: 
    base = int(input("base:"))
    height = int(input("Height:"))
    area = base * height / 2
    print(area)
  elif shape == 2: 
    length = int(input("Length:"))
    width = int(input("Width:"))
    area = length * width
    print(area)
  elif shape == 3:
    side = int(input("Side:"))
    area = side ** 2
    print(area)
  elif shape == 4:
    radius = int(input("Radius:"))
    area = pie * radius **2
    print(area)
  elif shape == 5:
    break
  else:
    print("Something went wrong")
0
John Gordon On

Generally, you can wrap the whole thing in another loop:

while True:
    # print menu choices
    # ask for input
    # calculate the answer
    again = input("Do you want another?")
    if again == "no":
        break
    # otherwise the outer loop will run again