Python 如何设置数据验证以将3个字段的输入限制为仅数字?

Python 如何设置数据验证以将3个字段的输入限制为仅数字?,python,validation,Python,Validation,几天前我刚刚被介绍到python 我需要三个不同字段的用户输入。如何将用户输入限制为所有三个问题的数字 while True: try: workinghours = int(raw_input("what is your working hours?")) except ValueError: print "Try again! what is your working hours?" continue norm

几天前我刚刚被介绍到
python

我需要三个不同字段的用户输入。如何将用户输入限制为所有三个问题的数字

while True:
    try:
        workinghours = int(raw_input("what is your working hours?"))
    except ValueError:
        print "Try again! what is your working hours?"
        continue

        normalrate = int(raw_input("what is your normal rate?"))
    except ValueError:
        print "Try again! what is your normal rate?"
        continue

        overtimerate = int(raw_input("what is your overtime rate"))
    except ValueError:
        print "Try again! what is your overtime rate?"
        continue

if workinghours > 40:
    overworkinghours = workinghours - 40
    overtimepayment = overtimerate * overworkinghours
    print ("your extra overtime salary is + $%.2f" % overtimepayment)
    normalpayment = normalrate * 40
    print ("your normal hours salary is + $%.2f" % normalpayment)
    totalsalary = float(normalpayment) + float(overtimepayment)
    print ("your total salary is: + $%.2f" % totalsalary)
else:

    normalpayment = normalrate * workinghours
    print ("your total salary without overtime is $%.2f" % normalpayment)
你一次又一次地向可怜的灵魂询问同样的信息,无法逃脱:) 除了while循环永远不会结束,因为任何地方都没有
break
语句之外,您还误解了
continue
语句。正如我们所看到的:

Python中的continue语句将控件返回到while循环的开头。continue语句拒绝循环当前迭代中的所有剩余语句,并将控件移回循环顶部

这意味着,如果用户犯了错误(输入类型错误),他将返回到第一个问题,而不是他在中犯了错误的问题

为每个字段分离while循环 您需要将其设置为三个独立的
while
循环,每个字段一个:

while True:
    try:
        workinghours = int(raw_input("what is your working hours? "))
    except ValueError:
        print "Try again! "
    else:
        break
    
while True:
    try:
        normalrate = int(raw_input("what is your normal rate? "))
    except ValueError:
        print "Try again! "
    else:
        break

while True:
    try:
        overtimerate = int(raw_input("what is your overtime rate? "))
    except ValueError:
        print "Try again! "
    else:
        break

if workinghours > 40:
    overworkinghours = workinghours - 40
    overtimepayment = overtimerate * overworkinghours
    print ("your extra overtime salary is + $%.2f" % overtimepayment)
    normalpayment = normalrate * 40
    print ("your normal hours salary is + $%.2f" % normalpayment)
    totalsalary = float(normalpayment) + float(overtimepayment)
    print ("your total salary is: + $%.2f" % totalsalary)
else:
    normalpayment = normalrate * workinghours
    print ("your total salary without overtime is $%.2f" % normalpayment)
你一次又一次地向可怜的灵魂询问同样的信息,无法逃脱:) 除了while循环永远不会结束,因为任何地方都没有
break
语句之外,您还误解了
continue
语句。正如我们所看到的:

Python中的continue语句将控件返回到while循环的开头。continue语句拒绝循环当前迭代中的所有剩余语句,并将控件移回循环顶部

这意味着,如果用户犯了错误(输入类型错误),他将返回到第一个问题,而不是他在中犯了错误的问题

为每个字段分离while循环 您需要将其设置为三个独立的
while
循环,每个字段一个:

while True:
    try:
        workinghours = int(raw_input("what is your working hours? "))
    except ValueError:
        print "Try again! "
    else:
        break
    
while True:
    try:
        normalrate = int(raw_input("what is your normal rate? "))
    except ValueError:
        print "Try again! "
    else:
        break

while True:
    try:
        overtimerate = int(raw_input("what is your overtime rate? "))
    except ValueError:
        print "Try again! "
    else:
        break

if workinghours > 40:
    overworkinghours = workinghours - 40
    overtimepayment = overtimerate * overworkinghours
    print ("your extra overtime salary is + $%.2f" % overtimepayment)
    normalpayment = normalrate * 40
    print ("your normal hours salary is + $%.2f" % normalpayment)
    totalsalary = float(normalpayment) + float(overtimepayment)
    print ("your total salary is: + $%.2f" % totalsalary)
else:
    normalpayment = normalrate * workinghours
    print ("your total salary without overtime is $%.2f" % normalpayment)

一定是我,但你所说的多重数据验证限制到底是什么意思?3个字段:工作时间、正常费率和加班费率。全部仅限于数字。像abc这样的字符将被提示重新输入。它必须是我,但您的确切意思是:多数据验证限制?3个字段:工作时间、正常费率和加班费率。全部仅限于数字。像abc这样的字符将被提示重新输入。