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

时间:2017-09-23 13:38:46

标签: python try-except

请告诉我它有什么问题。因为当我运行它时它会告诉错误。在线"如果小时> 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()

4 个答案:

答案 0 :(得分:1)

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

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

    • 要么设置例外名称,例如:

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

    • 或者不指定例外并将其留空
  5. 您输入代码的正确方法是:

    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()
    

    希望它有所帮助!

答案 1 :(得分:0)

一些错误......

你在if statement的末尾错过了一个冒号。此外,else不应该是indented。最后,我纠正了您的try except声明,因为您无缘无故地提升ValueError :)。现在,如果输入无法成功转换为ints,则ValueError将被引发,loop将继续(如果没有错误,则代码将继续并{{1}离开循环)。

所以最终的代码是:

break

答案 2 :(得分:0)

我认为这就是你要找的东西:

do_action( 'woocommerce_before_single_product_summary' );

答案 3 :(得分:-1)

你不应该使用加注但只是尝试除了+使用正确的缩进(如果一个else)将永远不会在你的情况下使用(仅在循环时)。试试版本:

while True:
hrs = input ("Enter no.of hrs worked: ")
rate = input ("Enter the rate per hour: ")
try:
    hrs = int(hrs)
    rate = int(rate)

except ValueError as e:
    print (e)
    print("Non numeric value")
    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)
相关问题