首页 > 解决方案 > 函数中的 Python 错误“netpay 未定义”

问题描述

我不断收到上面的错误,我的猜测是我没有正确传递信息

BASE_PAY=900

total_sale=float(input('What\'s your total sale?: '))

def main():
    
    demographics=get_info()
    income=get_income()
    budget=get_budget(netpay)
    print('Total sales is: $',total_sale)
    print('Your comission is: $',comission)
    print('The gross pay is: $',Gpay)
    print('The deductions are: $',deductions)
    print('The netpay is: $',netpay)
    print('Housing & Utility: $', HnC)
    print('Food & Clothing: $', FnC)
    print('Entertainment: $', entertainment)
    print('Miscellaneous costs: $', misc)

    def get_info():
        Fname=input('Enter your first name: ')
        Lname=input('Enter your last name: ')
        gender=input('Please enter your gender(m/f): ')
        if gender=='m' or gender =='M':
            print('Mr.',Fname,Lname)
        else:
            print('Ms.',Fname,Lname)
        return Fname, Lname, gender
    
    def get_income():
        comission=total_sale*0.06
        Gpay=BASE_PAY*comission
        deductions=Gpay*0.18
        netpay=Gpay-deductions
        return comission, Gpay, deductions, netpay
    
    def get_budget(netpay):
        HnC=netpay*0.45
        FnC=netpay*0.20
        entertainment=netpay*0.25
        misc=netpay*0.10
        return Hnc,FnC, entertainment, misc
    main()

标签: python

解决方案


你还没有netpay为你的函数定义get_budget,你已经在本地的另一个函数中定义了它get_income,所以在你试图调用它的地方,它是看不到的。您应该创建一个名为的全局变量netpay并将其声明为 None。然后你可以从你的函数内部编辑它并在你的get_income函数中调用它get_budget而不返回这个错误。

也许通读一下以了解python中的变量范围。https://www.w3schools.com/python/python_scope.asp


推荐阅读