Python 在处理同一个异常时,如何保护程序不受另一个异常的影响?

Python 在处理同一个异常时,如何保护程序不受另一个异常的影响?,python,python-3.x,exception-handling,Python,Python 3.x,Exception Handling,我得到: try: grossCheck = int(input("How much? (figures only pls.)\n")) except ValueError: grossCheck = int(input("How much? (FIGURES ONLY PLS.)\n")) tenPees = grossCheck * 0.1 realPees = grossCheck - tenPees print("you've got " + str(realPees

我得到:

try:
    grossCheck = int(input("How much? (figures only pls.)\n"))
except ValueError:
    grossCheck = int(input("How much? (FIGURES ONLY PLS.)\n"))

tenPees = grossCheck * 0.1
realPees = grossCheck - tenPees

print("you've got " + str(realPees))
问题是我之前处理过同样的异常。
我正在尝试处理它,以防用户在不中断程序的情况下多次输入错误的值。

您需要以某种方式处理异常。一种方法是不断地问:

ValueError: invalid literal for int() with base 10: 'w'

During handling of the above exception, another exception occurred:
这是处理错误的正确方法之一。
您得到的错误是因为您将输入放入了except block,您不应该这样做,在except block中您应该打印错误(如果需要),或者只是重复try block

您需要显示代码和完整的回溯。给定的数据不足。为什么要重复try and catch块中的代码,请检查我的答案以查看更好的错误处理方法我重复了OP repeated,我复制了他的功能,但修复了他询问的错误。还要注意的是,他在
输入中有不同的字符串。这没关系,您可以根据需要更改字符串,但虽然循环不应该在except块中,但如果您有5个错误例外而不是VALUERROR,那么您需要做的是,在每个except中,每个错误都有5个以上的例外,正如所说的(也许您需要被告知这一点)我只回答了他的问题。因此,它不是一个代码编写服务。我不想和你们争论,我只是建议你们两个,代码应该如何编写,我的代码确实回答了他的问题,因为它正确地处理了错误
try: 
    grossCheck = int(input("How much? (figures only pls.)\n")) 
except ValueError: 
    while True:
        try: 
            grossCheck = int(input("How much? (FIGURES ONLY PLS.)\n"))
            break
        except ValueError:
            pass
while 1:
    try:
        grossCheck = int(input("How much? (figures only pls.)\n"))
        tenPees = grossCheck * 0.1
        realPees = grossCheck - tenPees

        print("you've got " + str(realPees))
    except ValueError:
        print('You must enter number')