如何调用在python实际代码之后定义的函数?

2024-04-29 01:11:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想调用一个在调用之后定义的函数。但问题是,如果我把函数放在调用之前,它只会以错误的顺序调用程序

我没能尝试太多,因为它似乎不起作用。我试过查,但没有找到帮助

def mainmenu():
    choice = input("what would you like to use? press 1 for mathematics")
    if choice.lower() == '1':
        mathematics()
    else:
        breakpoint()
mainmenu()

def mathematics():
    firstnumber = int(input("Enter the first number: "))
    secondnumber = int(input("enter second number: "))
    result = firstnumber * secondnumber
    print(result)
    print("That is your multiplication answer.")
    ask = input("Would you like to try again? Y for yes, N for no: ")
    if ask.lower() == 'y':
        mathematics()
    else:
        print("Thankyou.")
mathematics()

上面只是一个例子代码,我正在尝试做什么。当您按1选择数学代码时,它只会给出一个错误,表示函数未定义。但是,如果我将mainmenu()代码与mathematics()代码切换,那么它首先显示数学,然后显示菜单


Tags: to函数代码youforinputifdef