Python 这个代码有什么问题?它给出了一个错误

Python 这个代码有什么问题?它给出了一个错误,python,Python,我试图根据家庭收入和孩子数量给出一个返还金额 当我运行它时,它会说: 回溯最近一次调用:第24行,在第5行的主printAmount returned:,AmoUnreturned name错误:未定义全局名称“returned”>>- 执行此操作时: amount(returned) 。。。您正在调用函数amount,并将返回的变量的值赋给它。在代码中,您没有定义返回的变量,这就是为什么会出现错误 写下您要做的事情的正确方法是将收入和子女(它们成为函数的输入)传递给函数,然后打印函数返回的内

我试图根据家庭收入和孩子数量给出一个返还金额

当我运行它时,它会说:

回溯最近一次调用:第24行,在第5行的主printAmount returned:,AmoUnreturned name错误:未定义全局名称“returned”>>-

执行此操作时:

amount(returned)
。。。您正在调用函数amount,并将返回的变量的值赋给它。在代码中,您没有定义返回的变量,这就是为什么会出现错误

写下您要做的事情的正确方法是将收入和子女(它们成为函数的输入)传递给函数,然后打印函数返回的内容:

print("Amount returned: ", amount(income, children))
这意味着您必须重新定义函数,以接受收入和子女作为输入:

def amount(income, children):
    ...
如果确实需要名为returned的变量,则应将其设置为函数的结果:

returned = amount(income, children)
print("Amount returned: ", returned)

@user3341166我虽然你正在学习编码,而且你有概念错误,注意你的问题,否则你会在这个网站上感到失望,记住问聪明的问题,我的意思是,阅读谷歌,每次当你遇到错误时,粘贴错误,但首先,阅读错误,有一段时间在描述上就是解决方法

你的错误在于函数的定义,当你定义一个函数时,你也定义了输入变量,或者没有输入,在你的情况下,你需要两个输入来传递收入和孩子,否则,你的函数就不知道该做什么

如果您检查您的函数,您使用的是两个变量income和children,但是该变量是在函数外定义的,您需要一种方法将它们传递给函数,然后您需要创建函数参数

我希望你能理解我的小介绍。祝你好运

def main():
    income = int(input("Please enter the annual household income: "))
    children = int(input("Please enter the number of children for each applicant: "))

    print("Amount returned: ", amount(income, children))


def amount (income, children):
    if income >= 30000 and income < 40000 and children >= 3:
        amnt = (1000 * children)
        return amnt

    elif income >= 20000 and income < 30000 and children  >= 2:
        a = (1500 * children)
        return a

    elif income < 20000 :
        r = (2000 * children)
        return r

    else:
        return "error"

if __name__ == '__main__':
    main()

它给出了什么错误?它应该做什么?不要只是在我们身上转储代码,请解释您希望我们做什么。当我运行它时,它会说:回溯最近的调用last:第24行,在第5行,在主printAmount returned:,AMONUTRETURNED name错误:未定义全局名称“returned”>>>是否定义了returned?它在main中是如何定义的?您需要删除returned或在某处定义它。错误消息的最后一行还声明:NameError:global name'returned'未定义如何定义返回的变量?我该如何定义它?
def main():
    income = int(input("Please enter the annual household income: "))
    children = int(input("Please enter the number of children for each applicant: "))

    print("Amount returned: ", amount(income, children))


def amount (income, children):
    if income >= 30000 and income < 40000 and children >= 3:
        amnt = (1000 * children)
        return amnt

    elif income >= 20000 and income < 30000 and children  >= 2:
        a = (1500 * children)
        return a

    elif income < 20000 :
        r = (2000 * children)
        return r

    else:
        return "error"

if __name__ == '__main__':
    main()