与 def main() 和 def menu() 顺序混淆

confused with def main() and def menu() order

在我选择一个选项后,我的程序应该在我输入任何形状的单位后给我矩形、圆形或三角形的面积。但是它并没有在一个面积公式之后停止,而是继续做所有这些。我该如何阻止呢?

import math

def main():
    menu()
    if choice ==1:
        circle()
    if choice == 2:
        rectangle()
    if choice ==3:
        triangle()


def menu():
    global choice
    choice = int(input('choose option 1-3:'))

    while choice < 1 or choice > 3:
        print('error. must choose option 1-3')
        choice = int(input('try again:'))




circle()

rectangle()

triangle()

def circle ():
    radCir = float(input('enter radius of circle:'))
    areaCir = math.pi*radCir**2

    print('area of circle:',format(areaCir,'.2f'))

def rectangle():
    global choice
    length = float(input('enter length of rectangle:'))
    width = float(input('enter width of rectangle:'))

    areaRec = length * width

    print('area of rectangle:',format(areaRec, '.2f'))

def triangle():
    base = float(input('enter base of triangle:'))
    height = float(input('enter height of triangle:'))

    areaTri = base * height * .5

    print('area of triangle:',format(areaTri,'.2f'))


main()

发生这种情况是因为您在定义函数之前调用它们。在定义之前删除对函数的调用,即 delete:

circle()

rectangle()

triangle()

出现在 def circle ():....

的正上方