Python 询问用户想要什么样的功能

Python 询问用户想要什么样的功能,python,Python,我有一个面积计算器,我希望用户在开始时选择要计算的内容,而不是从列表中往下看。是否有用于询问用户并停止其他代码的代码。在用户选择后,将用户带到该特定功能。然后,将他们带回询问屏幕?我需要一些建议 import math def square(): print ("Area of Square") print ("") S = float(input("Enter Length of Square:")) A = round((S**2),3) print

我有一个面积计算器,我希望用户在开始时选择要计算的内容,而不是从列表中往下看。是否有用于询问用户并停止其他代码的代码。在用户选择后,将用户带到该特定功能。然后,将他们带回询问屏幕?我需要一些建议

import math

def square():
    print ("Area of Square")
    print ("")
    S = float(input("Enter Length of Square:"))
    A = round((S**2),3)
    print (A, "is the Area of the Square")
    print("")
    print("")
square()

def rectangle():
    print("Area of Rectangle")
    print ("")
    L = float(input("Enter Length of Rectangle:"))
    W = float(input("Enter Width of Rectangle:"))
    A = round((L*W),3)
    print (A, "is the Area of the Rectangle")
    print("")
    print("")
rectangle()

def paralelogram():
    print("Area of Paralelogram")
    print("")
    B = float(input("Enter Base of Paralelogram:"))
    H = float(input("Enter Height of Paralelogram:"))
    A = round((B*H),3)
    print (A, "is the Area of the Paralelogram")
    print("")
    print("")
paralelogram()

def triangle():
    print("Area of Triangle")
    print("")
    B = float(input("Enter Base of Triangle:"))
    H = float(input("Enter Height of Triangle:"))
    A = round(((B*H)/2),3)
    print (A, "is the Area of the Triangle")
    print("")
    print("")
triangle()

def circle():
    print("Area of Circle")
    print("")
    r = float(input("Enter Radius of Circle:"))
    A = round(math.pi*(r**2),3)
    print (A, "is the Area of the Circle")
    print("")
    print("")
circle()

def trapezoid():
    print("Area of Trapezoid")
    print("")
    B1 = float(input("Enter Base 1 of Trapezoid:"))
    B2 = float(input("Enter Base 2 of Trapezoid:"))
    H = float(input("Enter Height of Trapezoid:"))
    A = round((((B1+B2)/2)*H),3)
    print (A, "is the Area of the Trapezoid")
    print("")
    print("")
trapezoid()

def sphere():
    print("Area of Sphere")
    print("")
    r = float(input("Enter Radius of Sphere:"))
    A = round((((r**2)*4)*math.pi),3)
    print (A, "is the Area of the Sphere")
    print("")
sphere()

你可以通过一个while循环来实现

#your functions here

options="""1-)Square
2-)Rectangel
3-)Paralelogram
4-)Triangle
5-)Trapezoid
6-)sphere
"""
while True:
    print (options)
    user=input("Please choose a valid option: ")
    if user=="1":
        square()
        continue
    elif user=="2":
        rectangel()
        continue
    ..
    ..
    ..  #same codes with elif statement till 6.
    ..
    else:
        print ("It's not a valid choose.Please choose a valid one.")

一种更具python风格的方法是在字典中存储对函数的引用:

area_func = {'triangle': triangle, 'square': square, 'rectangle': rectangle,
             'paralelogram': paralelogram, 'circle': circle, 'trapezoid': trapezoid,
             'sphere': sphere}

while True:
    shape = input('What shape do you want to calculate the area of ("stop" to end)?')
    if shape == 'stop':
        break
    try:
        area_func[shape]()
    except KeyError:
        print("I don't recognise that shape.")
        continue
这是因为函数和Python中的其他东西一样,都是对象。因此,您可以将它们存储为字典中的值,为它们指定变量名,等等。表达式
area_func[shape]
因此是对函数的引用,例如,
triangle
,然后可以调用该函数(通过附加
()
,这是一组空括号,因为您的函数不带任何参数)


正如其他人所指出的,您可能不希望在每个定义之后调用函数。而且,说得迂腐一点,平行四边形是正确的拼写

为什么要调用所有函数?它们不是类。是的,有代码可以这样做,你应该试着自己编写。你的问题是什么?你试过什么?你犯了什么错误?你可以打印出一个菜单,等待输入,解析输入,然后调用该函数。你也可以用一个简单的while-True:和if-something:调用某个函数。谢谢你的反馈!为什么在
区域函数[shape]()
中使用
()
,请参见我最新编辑中的说明。
()
将对函数的引用转换为对函数的调用。