如何在python中使用try except语句处理非数字输入

2024-03-28 15:10:04 发布

您现在位置:Python中文网/ 问答频道 /正文

请告诉我它有什么毛病。因为当我运行这个程序时,它会显示一个错误。在“if hrs>;40”行中显示语法错误!在

while true:
    hrs = input ("Enter no.of hrs worked: ")
    rate = input ("Enter the rate per hour: ")
    try:
        hrs = int(hrs)
        rate = int(rate)
        raise ValueError("Non numeric value")
    except enter code hereValueError as e:
        print (e)
        continue
if hrs > 40
    # anything over 40 hrs earns the overtime rate
    overtimeRate = 1.5 * rate
    overtime = (hrs-40) * overtimeRate
    # the remaining 40 hrs will earn the regular rate
    hrs = 40
    regular=hrs*rate
    total_pay=regular+overtime
    print(total_pay)
    else:
        # if you didn't work over 40 hrs, there is no overtime
        overtime = 0
        total_pay=hrs*rate
        print(total_pay)
  quit()

Tags: thenoinputifratepayoverint
3条回答

您的代码包含一些语法和语义错误:

  1. 布尔值true应该以大写字母True开头。在
  2. 编写代码的方式非常重要,并且应该以正确的方式进行格式化,每个指令前的空格是敏感的,即同一块代码的前面应该有相同数量的空格。在
  3. 通过在trybloc中使用raise,您创建了一个自定义异常,该异常将始终被执行,并且永远不会到达{}bloc。在
  4. except关键字之后,有两个选项:

    • 输入异常名称,例如:

    except ValueError: print("Non-numeric data found in the file.")

    • 或者不指定异常,让它空白

键入代码的正确方法是:

while True:
    hrs = input ("Enter no.of hrs worked: ")
    rate = input ("Enter the rate per hour: ")
    try:
        hrs = int(hrs)
        rate = int(rate)
        #raise ValueError("Non numeric value")
    except :
        print ('Non numeric data found.')
        continue    
    if hrs > 40:
        # anything over 40 hrs earns the overtime rate
        overtimeRate = 1.5 * rate
        overtime = (hrs-40) * overtimeRate
        # the remaining 40 hrs will earn the regular rate
        hrs = 40
        regular=hrs*rate
        total_pay=regular+overtime
        print(total_pay)
    else:
        # if you didn't work over 40 hrs, there is no overtime
        overtime = 0
        total_pay=hrs*rate
        print(total_pay)
    quit()

希望有帮助!在

几个错误

您在if statement的末尾缺少一个冒号。另外,else不应该是indented。最后,我更正了您的try except语句,因为您无缘无故地提出了ValueError。所以现在,如果输入不能成功地转换为ints,那么ValueError将被引发,loop将继续(如果没有错误,则代码将继续,并且break退出循环)。在

所以最后的代码是:

while True:
    hrs = input ("Enter no.of hrs worked: ")
    rate = input ("Enter the rate per hour: ")
    try:
        hrs = int(hrs)
        rate = int(rate)
        break
    except ValueError:
        print("invalid entries")
        continue
if hrs > 40:
    # anything over 40 hrs earns the overtime rate
    overtimeRate = 1.5 * rate
    overtime = (hrs-40) * overtimeRate
    # the remaining 40 hrs will earn the regular rate
    hrs = 40
    regular=hrs*rate
    total_pay=regular+overtime
    print(total_pay)
else:
    # if you didn't work over 40 hrs, there is no overtime
    overtime = 0
    total_pay=hrs*rate
    print(total_pay)

我想这就是你想要的:

while True:
    hrs = input ("Enter no.of hrs worked: ")
    rate = input ("Enter the rate per hour: ")
    try:
        hrs = int(hrs)
        rate = int(rate)
        break
    except ValueError as e:
        print (e)
        continue
if hrs > 40:
    # anything over 40 hrs earns the overtime rate
    overtimeRate = 1.5 * rate
    overtime = (hrs-40) * overtimeRate
    # the remaining 40 hrs will earn the regular rate
    hrs = 40
    regular=hrs*rate
    total_pay=regular+overtime
    print(total_pay)
else:
    # if you didn't work over 40 hrs, there is no overtime
    overtime = 0
    total_pay=hrs*rate
    print(total_pay)
quit()

相关问题 更多 >