Home > Software design >  Python error "netpay not defined" in functions
Python error "netpay not defined" in functions

Time:10-27

i keep getting the error above, my guess is that i'm not passing the info correctly

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

CodePudding user response:

You have not defined netpay for your function get_budget, you have defined it inside of another function locally get_income and so where you are trying to call it, it cannot be seen. You should create a global variable called netpay and declare it as None. Then you can edit it from inside your get_income function and call it in your get_budget function without returning this error.

Maybe have a read through this to gain an understanding of variable scope in python. https://www.w3schools.com/python/python_scope.asp

  • Related