Python 3.x. I'm trying to make a payroll program that accepts input for the amount of hours worked by each employee, and multiplies it by their predetermined pay. It error traps properly for if the user enters an invalid input, like "abc" for hours. However it won't accept float values. I understand that isdigit() isn't working for decimals because the "." returns a false value inside isdigit(). How can I modify this program to accept both int and float values as valid input?
#This is a program to calculate payroll expenses.
employees = ['Oscar', 'Judy', 'Sandra', 'Tino', 'Andres', 'Rich', 'Matt', 'Daniel', 'Natalie', 'Elena']
employeeHourlyPay = [14.5, 14.5, 13.5, 13.0, 13.0, 11.0, 11.0, 10.0, 9.0, 10.0]
employeeHours = 
totalPay = 
#Iterate through employees and ask for hours worked. Program will check for
#valid digit inputs, and prompt you to only enter digits when anything else
#is entered as input.
#****Fix to accept decimals****#
for i in employees:
print('Enter hours for', i , ':')
x = str(input())
print('Please use numbers or decimals only.')
#Calculate pay per employee and add to list.
for i, j in zip(employeeHourlyPay, employeeHours):
totalPay.append(i * float(j))
#Display pay per employee by iterating through employees and totalPay.
for i, j in zip(employees, totalPay):
print(i + "'s pay is", str(j))
#Calculate and display total payroll by summing items in totalPay.
print('Total Payroll: ' + str(sum(totalPay)))
Better say that you're sorry than asking for permission... Just try to get a
float out of the input and well... If you get an exception, that's because it's not a float:
for i in employees: while True: print('Enter hours for', i, ':') try: x = float(input()) print("You entered x=%s" % x) employeeHours.append(x) break except ValueError: print('Please use numbers or decimals only.') continue