回溯错误:无法将序列乘以非整数类型'float'?[Python 3.2.5.1]

-1 投票
1 回答
504 浏览
提问于 2025-04-18 05:48

错误追踪(最近的调用在最后): 文件 "E:/Portable Python 3.2.5.1/Tasks/Wk7",第16行,出错位置:

monthlySalary = hourPay * 0.1

类型错误:不能用非整数类型的浮点数去乘一个序列

#Input hoursWorked
#Input hourPay
#Ask “Consultancy income? (Leave blank if none)”
#   If value:
#       Input consultFee
#       monthlySalary = ((hourPay - (hourPay * 0.1)) * hoursWorked) + (consultFee – (consultFee * 0.2))
#   Elif no value:
#       monthlySalary = hoursWorked * hourPay
#print (“The monthly wage is $”,monthlySalary)

hoursWorked = input ("Hours worked: ")
hourPay = input ("Hour pay: $")

consultFee = input ("Consultant fee? (Leave blank if none): $")
consultFee == ''
monthlySalary = hourPay * 0.1
'' == False
monthlySalary = ((hourPay - (hourPay * 0.1)) * hoursWorked) + (consultFee - (consultFee * 0.2))

print ("The monthly salary is: $",(int(monthlySalary)))

1 个回答

0

monthlySalary = hourPay * 0.1

改成

monthlySalary = float(hourPay) * 0.1

原因是,input 或 raw input 接收到的是字符串,你必须先把它们转换成正确的类型。

撰写回答