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

Python 与def main()和def menu()顺序混淆,python,function,loops,menu,main,Python,Function,Loops,Menu,Main,在我选择一个选项后,我的程序应该在我输入任何形状的单位后给我一个矩形、圆形或三角形的面积。但它并没有在一个面积公式后停止,而是继续进行所有的计算。我该怎么阻止这一切 import math def main(): menu() if choice ==1: circle() if choice == 2: rectangle() if choice ==3: triangle() def menu():

在我选择一个选项后,我的程序应该在我输入任何形状的单位后给我一个矩形、圆形或三角形的面积。但它并没有在一个面积公式后停止,而是继续进行所有的计算。我该怎么阻止这一切

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()
导入数学
def main():
菜单()
如果选项==1:
圈()
如果选项==2:
矩形()
如果选项==3:
三角形()
def菜单():
全球选择
choice=int(输入('choose option 1-3:'))
当选项<1或选项>3时:
打印('错误。必须选择选项1-3')
choice=int(输入('重试:'))
圈()
矩形()
三角形()
def circle():
radCir=float(输入('输入圆的半径:'))
areaCir=math.pi*radCir**2
打印('圆的面积:',格式(areaCir,.2f'))
def矩形()
全球选择
长度=浮点(输入('输入矩形的长度:'))
宽度=浮动(输入('输入矩形的宽度:'))
areaRec=长度*宽度
打印('矩形区域:',格式(areaRec,.2f'))
def三角形():
base=float(输入('输入三角形的底:'))
高度=浮动(输入('输入三角形的高度:'))
areaTri=基础*高度*.5
打印('三角形区域:',格式(areaTri,'.2f'))
main()

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

circle()

rectangle()

triangle()

发生在
def circle():…

circle()
rectangle()
triangle()
正上方的是模块级函数调用,它们与您试图在函数内部控制的代码流完全无关(换句话说,无论您在
main
menu
函数中执行什么操作,它们都将被调用)。您应该根据输入调用其中一个函数,您已经在
main()中实现了该函数
此代码不会产生您描述的错误;它会产生一个
NameError
,因为您试图在定义它之前调用
circle