pythonif-statement

I want to take output from a function to use in a IF statement in another function


I know what cause is of the problem. When I loop through the if/else statement, the function gets called again and asks the question again. From line 10 - 35.

I am really unsure how to ask for help with this one. I know why its broken, I just have no idea how to ask to fix it.

Here is all the code. Just a disclaimer. I am very new to development.

def tax_calc(tax: float, tax_perc: int):
    tax_pay = tax * (tax_perc / 100)
    return tax_pay


def income_bracket_calc(user_sal: float):

    def benefit_calc():
        user_child_input = input("Do you have any children? (y/n): ")

        if user_child_input == "y":
            return 1
        elif user_child_input == "n":
            return 2
        else:
            return 3

    if user_sal <= 0:
        print("Invalid input. Please Select y/n only.")

    elif 1 <= user_sal <= 5000:
        benefit_calc()

        if benefit_calc() == 1:
            print("You have qualified for free medical aid and a 100% school bursary for your children.")
            print("You do not have to pay income tax.")
            print()

        elif benefit_calc() == 2:
            print("You have qualified for free medical aid.")
            print("You do not have to pay income tax.")
            print()

        elif benefit_calc() == 3:
            print("Invalid input. Please use y/n only.")
            print()

    elif 5001 <= user_sal <= 10000:

        print()

    elif user_sal > 10000:
        print()


while True:
    user_income_input = float(input("Please enter your income: "))

    income_bracket_calc(user_income_input)

    kill_prg = input("Would you like to exit the program? y/n: ")
    if kill_prg == "y":
        break
    elif kill_prg == "n":
        continue
    else:
        print("Invalid input. Use y/n only.")

I would want the if statements, in the "income_bracket_calc" (line 23-38), to work without calling the "benefit_calc()" function again.

As I said, I am really not sure how to ask this question. Or if what I want to do is even possible


Solution

  • Try changing your income_bracket_calc function like this:

    def income_bracket_calc(user_sal: float):
    
        def benefit_calc():
            user_child_input = input("Do you have any children? (y/n): ")
    
            if user_child_input == "y":
                return 1
            elif user_child_input == "n":
                return 2
            else:
                return 3
    
        if user_sal <= 0:
            print("Invalid input. Please Select y/n only.")
    
        elif 1 <= user_sal <= 5000:
            result = benefit_calc()
    
            if result == 1:
                print("You have qualified for free medical aid and a 100% school bursary for your children.")
                print("You do not have to pay income tax.")
                print()
    
            elif result == 2:
                print("You have qualified for free medical aid.")
                print("You do not have to pay income tax.")
                print()
    
            elif result == 3:
                print("Invalid input. Please use y/n only.")
                print()
    
        elif 5001 <= user_sal <= 10000:
    
            print()
    
        elif user_sal > 10000:
            print()