Python 3.x.私は各従業員が働いていた時間の入力を受け取り、それを所定の給料で掛ける給与計算プログラムを作成しようとしています。ユーザーが無効な入力を入力した場合は、トラップが正しくトラップされます(たとえば、数時間 "abc"など)。ただし、浮動小数点値は受け入れません。私はisdigit()が小数点のために働いていないことを理解しています。 isdigit()の内部で偽の値を返します。このプログラムを変更して、int値とfloat値の両方を有効な入力として受け入れることができますか?Python 3:給与計算プログラムの小数点を受け入れる
#! python3
#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:
while True:
print('Enter hours for', i , ':')
x = str(input())
if x.isdigit():
employeeHours.append(float(x))
break
else:
print('Please use numbers or decimals only.')
continue
#***End Fix***
#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)))