Kael Tee Kael Tee - 2 months ago 9
Python Question

How can I set up data validation to restrict input for 3 fields to number only?

I was just introducted to

python
a few days ago

I need user input for three different fields. How can I restrict user input to numbers for all three questions?

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)

Answer

You're asking the poor soul the same information over and over again, no escape is possible :)

Apart from the fact that the while loop never ends because there is no breakstatement anywhere, you are misunderstanding the continue statement. As we can read here:

The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

That means that if your user makes a mistake (wrong type of input), he returns to the first question, instead of the question he made the mistake in.

Separate while loop foor each field

You need to make it three separate while loops, one for each field:

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)