在函数中传递 'ValueError' & 'continue' 并调用它

Passing 'ValueError' & 'continue' in a function and call it

我正在尝试检查用户输入以确保: 1)是一个浮点数 2) 浮点数不为负数

我正在尝试将以上 2 个检查放入函数中,并在用户输入变量后调用它。

但是,我似乎无法将 'ValueError' & 'continue' 放入我可以调用的函数中。这可能吗?

我已经尝试了下面的代码,但是当我为 salCredit 或接下来的几个变量中的任何一个键入 't' 时,它会从顶部重复。如果我为每个变量重复 'ValueError' & 'continue',代码将起作用。我只是想知道是否有更短的方法?

def interestCalculator():

    #User inputs required for calculation of interest earned.
    while True:
        try: 
            mul_AccBal = float(input("Enter your Account Balance: "))
            #checkInputError(accBal)

            salCredit = float(input("Enter your Salary: "))
            #checkInputError(salCredit)

            creditCard = float(input("Credit Card Spend (S$): "))
            #checkInputError(creditCard)

        except ValueError:
            print("Please enter a valid number.")
            continue

def checkInputError(userInput):
    if userInput < 0:
        print("Please enter a positive number.")

interestCalculator()

预期结果:

场景 1:如果用户输入 't'

Enter your Account Balance: 5000
Enter your Salary: t 
Please enter a valid number.
Enter your Salary: 500

场景 2:如果用户输入负数

Enter your Account Balance: 5000
Enter your Salary: -50 
Please enter a valid number.
Enter your Salary: 500

当前结果:

场景 1:如果用户输入 't'

Enter your Account Balance: 5000
Enter your Salary: t 
Please enter a valid number.
Enter your Account Balance: 

场景 2:如果用户输入负数

Enter your Account Balance: 5000
Enter your Salary: -50 
Please enter a positive number.
Credit Card Spend (S$):

如果所有输入都成功,你需要break你的while循环(另请注意while循环末尾的continue是不必要的)。如果你想分别对每个数字进行验证,你可以这样做:

def get_float(message, retry_message="Please enter a valid number."):
    while True:
        try:
            ret = float(input(message))
            if ret >= 0:
                return ret
            else:
                print(retry_message)
        except ValueError:
            print(retry_message)

def interestCalculator():

    mul_AccBal = get_float("Enter your Account Balance: ")
    salCredit = get_float("Enter your Salary: ")
    creditCard = get_float("Credit Card Spend (S$): ")

试试这个:

    def interestCalculator():

    #User inputs required for calculation of interest earned.
    while True:
        invalid = True
        while invalid:
            try: 
                mul_AccBal = float(input("Enter your Account Balance: "))
                invalid=checkInputError(salCredit)
            except ValueError:
                print("Please enter a valid number.")
                continue
        invalid = True
        while invalid:
            try:
                salCredit = float(input("Enter your Salary: "))
                invalid=checkInputError(salCredit)
            except ValueError:
                print("Please enter a valid number.")
                continue
        invalid = True
        while invalid:
            try:
                creditCard = float(input("Credit Card Spend (S$): "))
                invalid=checkInputError(salCredit)
            except ValueError:
                print("Please enter a valid number.")
                continue


def checkInputError(userInput):
    if userInput < 0:
        print("Please enter a positive number.")
        return True
    return False

interestCalculator()

您可以创建一个持续提示输入直到输入有效浮点数的函数

def get_float_input(prompt):
    while True:
        try:
            user_input = float(input(prompt))
            if user_input < 0:
                print("Please enter a positive number.")
                continue  # start the while loop again
            return user_input  # return will break out of the while loop
        except ValueError:
            print("Please enter a valid number.")

mul_AccBal = get_float_input("Enter your Account Balance: ")
salCredit = get_float_input("Enter your Salary: ")
creditCard = get_float_input("Credit Card Spend (S$): ")