Home > Blockchain >  Trying to use an input of function_A within another function
Trying to use an input of function_A within another function

Time:07-08

I am trying to use an int input from the anual_salary function to calculate an answer for the fed_tax function but I keep getting an error saying the saved variable from function 1 is undefined in function 2.

I understand that salary is locally scoped but I need to use it in the next function along with other functions.

def anual_salary():
    salary = int(input("Enter your salary: "))
    print(f"Gross income is ${salary}")
    print(" ")
    return salary

def fed_tax():
    esbi = input("Are you an Employee, Self Employed, Business owner, or Investor? ")
    if esbi == "Employee" or "employee":
        fed_income_tax = .37 * salary
    elif esbi == "Self Employed" or "self employed":
        fed_income_tax = .35 * salary
    elif esbi == "Business owner" or "business owner":
        fed_income_tax = .20 * salary
    elif esbi == "Investor" or "investor":
        fed_income_tax = .15 * salary
    else:
        print("Incorrect answer!")
    fed_income_tax = round(fed_income_tax, 2)
    print(f"Your Federal Income Tax is ${fed_income_tax}")
    print(" ")
    return fed_income_tax

What am I doing wrong?

CodePudding user response:

You need to call the function and store the value in a local variable in fed_tax().

def fed_tax():
    salary = anual_salary()
    esbi = input("Are you an Employee, Self Employed, Business owner, or Investor? ")
    if esbi == "Employee" or "employee":
        fed_income_tax = .37 * salary
    elif esbi == "Self Employed" or "self employed":
        fed_income_tax = .35 * salary
    elif esbi == "Business owner" or "business owner":
        fed_income_tax = .20 * salary
    elif esbi == "Investor" or "investor":
        fed_income_tax = .15 * salary
    else:
        print("Incorrect answer!")
    fed_income_tax = round(fed_income_tax, 2)
    print(f"Your Federal Income Tax is ${fed_income_tax}")
    print(" ")
    return fed_income_tax

CodePudding user response:

If you want to use salary in other functions, you can just call the annual_salary().

    def anual_salary():
        salary = int(input("Enter your salary: "))
        print(f"Gross income is ${salary}")
        print(" ")
        return salary

    def fed_tax():
        salary = anual_salary()    # this line calls the anual_salary
        esbi = input("Are you an Employee, Self Employed, Business owner, or Investor? ")
        if esbi == "Employee" or esbi == "employee":
            fed_income_tax = .37 * salary
        elif esbi == "Self Employed" or esbi == "self employed":
            fed_income_tax = .35 * salary
        elif esbi == "Business owner" or esbi == "business owner":
            fed_income_tax = .20 * salary
        elif esbi == "Investor" or esbi == "investor":
            fed_income_tax = .15 * salary
        else:
            print("Incorrect answer!")
        fed_income_tax = round(fed_income_tax, 2)
        print(f"Your Federal Income Tax is ${fed_income_tax}")
        print(" ")
        return fed_income_tax

As shown above, whenever you want to use salary along with other functions, just need to add salary=anual_salary() to call it.

Hope this could help you a little bit.

CodePudding user response:

You are defining a function where you ask for a salary and it is returned, so, what you have to do is that in the function that multiplies said salary, you send to call the class that asks for it, in the following way:

def fed_tax():
salary = anual_salary()
esbi = ....
if ....
    fed_income_tax = .37 * salary
elif ...
    fed_income_tax = .35 * salary
elif ....
    fed_income_tax = .20 * salary
elif ....
    fed_income_tax = .15 * salary
else:
    print("Incorrect answer!")
fed_income_tax = round(fed_income_tax, 2)
print(f"Your Federal Income Tax is ${fed_income_tax}")
print(" ")
return fed_income_tax

CodePudding user response:

You can call the function as part of the if/else and avoid storing it as a separate value

def fed_tax():
esbi = input("Are you an Employee, Self Employed, Business owner, or Investor? ")
if esbi == "Employee" or "employee":
    fed_income_tax = 0.37* anual_salary()
elif esbi == "Self Employed" or "self employed":
    fed_income_tax = .35 * anual_salary()
elif esbi == "Business owner" or "business owner":
    fed_income_tax = .20 * anual_salary()
elif esbi == "Investor" or "investor":
    fed_income_tax = .15 * anual_salary()
else:
    print("Incorrect answer!")
fed_income_tax = round(fed_income_tax, 2)
print(f"Your Federal Income Tax is ${fed_income_tax}")
print(" ")
return fed_income_tax
  • Related