Search code examples
pythonfunctionloopsmenuprogram-entry-point

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


After I pick an option my program is supposed to give me the area of a rectangle, circle, or triangle after I input the unit of whatever shape. But instead of stopping after one area formula, it continues to do all of them. How do I stop this?

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()

Solution

  • This is happening because you are calling functions before you define them. Remove the calls to the functions before the definitions i.e. delete:

    circle()
    
    rectangle()
    
    triangle()
    

    that occur right above def circle ():....