【问题标题】:In Python: How to evaluate an exception, accept second iteration of user input, then run the second input against the exceptions again?在 Python 中:如何评估异常,接受用户输入的第二次迭代,然后再次针对异常运行第二次输入?
【发布时间】:2022-01-05 06:59:19
【问题描述】:

我创建了这个计算器来处理一些异常,但我想知道如何让异常的用户输入再次针对异常进行评估?例如,如果用户在第一次输入时输入了一个字母,则会执行 Value 错误。但是如果用户第二次输入一个字母,该函数不会循环备份并重新评估。如果我使用“继续”,它会从第一行开始寻找用户输入。我希望程序再次针对异常评估任何异常输入。有没有办法做到这一点?

class RangeValueError(Exception): 

    pass

while True:
    
    try: 
        first_num = int(input("Enter a first number between -100 and 100: "))
        if first_num not in range(-100, 100):
            raise RangeValueError 
    except ValueError:  
         first_num = input("That is not a number, please try again: ")
         first_num = int(first_num)
    except RangeValueError: 
        print(input("You must enter a number between -100 and 100, please try again: "))
    else:
        if first_num in range(-100, 100): 
            print("You chose", first_num, "for your first number.")
        pass

    try:
        sec_num = int(input("Enter a second number between -100 and 100: ")) 
        if sec_num not in range(-100, 100):
            raise RangeValueError
        if sec_num == 0:
            raise ZeroDivisionError
    except ZeroDivisionError:
        sec_num = input("You cannot use zero as your second number, please input a different number: ")
    
    except ValueError:
        sec_num = input("That is not a number, please try again: ")
        sec_num = int(sec_num)
    
    except RangeValueError:
        print(input("You must enter a number between -100 and 100, please input second number again: "))
    
    else:
        if sec_num in range(-100, 100): 
            print("You chose", sec_num, "for your second number.")
        pass
    
    first_num = float(first_num)
    sec_num = float(sec_num)     
                        
    sum_total = (first_num + sec_num)
    minus_total = (first_num - sec_num)
    product_total = (first_num * sec_num)
    divide_total = (first_num / sec_num)
    
    print("You chose to calculate", first_num, "and", sec_num, "here are your results: ")
    print()
    print()
    print("The sum total of", first_num, "plus", sec_num, "=", float(sum_total))
    print()
    print("The difference of", first_num, "minus", sec_num, "=", float(minus_total))
    print()
    print("The product of", first_num, "times", sec_num, "=", product_total)
    print()
    print("The quotient of", first_num, "divided by", sec_num, "=", divide_total)
    print()

    choice = input("Would you like to use the calculator again? Y or N? ")
    if choice == ('y'):
        continue
    if choice == ('n'):
        break

【问题讨论】:

    标签: python-3.x exception


    【解决方案1】:

    不要打印input() 的结果。相反,将其保存到您要保存到 try 块中的变量中。例如,

    first_num = int(input("You must enter a number between -100 and 100, please try again: "))
    

    【讨论】:

    • 谢谢,我试试看!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    相关资源
    最近更新 更多