我已经用 Python 编写了所有数学函数,但是当用户提示时无法让程序转到正确的选项
print ("""Calculations Menu:
1) Area (Square)
2) Area (Rectangle)
3) Area (Circle)
4) Perimeter (Square)
5) Perimeter (Rectangle)
6) Perimeter (Circle)
7) Exit
""")
choice = input("Input Menu Choice (1,2,3,4,5,6 or 7)?\n")
if choice == "1":
print ("You have chosen Area (Square)")
def area_square (width, height):
return width * height
def positive_input (prompt):
number = float(input(prompt))
while number <=0:
print ("Must be a positive number.")
number = float(input(prompt))
return number
w = positive_input ("Width: ")
h = positive_input ("Height: ")
print ("Width = ", w, "Height = ", h, "So Area =", area_square(w, h))
if choice == "2":
print ("You have chosen Area (Rectangle)")
def area_rectangle (width, height):
return width * height
def positive_input (prompt):
number = float(input(prompt))
while number <=0:
print ("Must be a positive number.")
number = float(input(prompt))
return number
w = positive_input ("Width: ")
h = positive_input ("Height: ")
print ("Width = ", w, "Height = ", h, "So Area =", area_rectangle(w, h))
if choice == "3":
print ("You have chosen Area (Circle)")
def area_circle (radius, pi):
return radius**2 * pi
def positive_input (prompt):
number = float(input(prompt))
while number <=0:
print ("Must be a positive number.")
number = float(input(prompt))
return number
radius = positive_input ("radius: ")
pi = 3.14159265
print ("Radius = ", radius, "Pi = ", pi, "So Area =", area_circle(radius, pi))
if choice == "4":
print ("You have chosen Perimeter (Square)")
def perimeter_square (side, ):
return side * 4
def positive_input (prompt):
number = float(input(prompt))
while number <=0:
print ("Must be a positive number.")
number = float(input(prompt))
return number
side = positive_input ("side: ")
print ("side = ", "So Perimeter =", perimeter_square(side,))
if choice == "5":
print ("You have chosen Perimeter (Rectangle)")
def perimeter_rectangle (sideA, sideB,):
return (sideA + sideB) * 2
def positive_input (prompt):
number = float(input(prompt))
while number <=0:
print ("Must be a positive number.")
number = float(input(prompt))
return number
sideA = positive_input ("Length: ")
sideB = positive_input ("Width: ")
print ("Length = ", sideA, "Width = ", sideB, "So Perimeter =", perimeter_rectangle(sideA, sideB))
if choice == "6":
print ("You have chosen Perimeter (Circle)")
def perimeter_circle (diameter, pi):
return diameter * pi
def positive_input (prompt):
number = float(input(prompt))
while number <=0:
print ("Must be a positive number.")
number = float(input(prompt))
return number
diameter = positive_input ("Diameter: ")
pi = 3.14159265
print ("Diameter = ", diameter, "Pi = ", pi, "So Perimeter =", perimeter_circle(diameter, pi))