我想从一个函数获取输出以在另一个函数的 IF 语句中使用

问题描述 投票:0回答:1

我知道问题的原因是什么。当我循环执行 if/else 语句时,该函数会再次被调用并再次询问问题。从 10 号线到 35 号线。

我真的不知道如何寻求帮助。我知道为什么它坏了,我只是不知道如何要求修复它。

这是所有代码。只是免责声明。我对开发非常陌生。

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.")

我希望“venue_bracket_calc”(第 23-38 行)中的 if 语句能够正常工作,而无需再次调用“benefit_calc()”函数。

正如我所说,我真的不知道如何问这个问题。或者如果我想做的事情是可能的

python if-statement
1个回答
0
投票

尝试像这样更改你的 Income_bracket_calc 函数:

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()
© www.soinside.com 2019 - 2024. All rights reserved.