Python - ValueError: could not convert string to float: '52.04121.43433.681035'

Python - ValueError: could not convert string to float: '52.04121.43433.681035'

这是我的代码(有点长,可以简化)

hours = float(input("Enter the number of hours worked in a week:"))
dep = float(input("Enter the number of dependents you have:"))

rate = 16.68
ovrate = 25.02


if hours<=40:
  gross = "{:.2f}".format(float(hours * rate))
else:
  gross = "{:.2f}".format(float((40*rate)+((hours-40)*ovrate)))
gross2 = float(gross)
print("Gross pay: $",gross)

ss = "{:.2f}".format(gross2*.06)
print("Social Security tax: $",ss)

federal = "{:.2f}".format(gross2*0.14)
print("Federal income tax: $",federal)

state = "{:.2f}".format(gross2*0.5)
print("State income tax: $",state)

union=10
print("Union dues: .00")
unionString=str(union)

if dep>=3:
  family = 35
  print("Family health insurance: .00 (additional insurance premiums for your family)")
else:
  family = 0
familyString = str(family)

netDed = ss+federal+state+unionString+familyString
netDedFloat = float(netDed)
netDed2 = "{:.2f}".format(netDedFloat)
print("Total deductions: $",netDed2)

netPay = gross2-netDed
print("Net pay: $",netPay)

当我 运行 它时,我收到以下消息:

Traceback (most recent call last):
  File "main.py", line 39, in <module>
    netDedFloat = float(netDed)
ValueError: could not convert string to float: '52.04121.43433.681035'

我已经尝试了我在网上阅读的所有内容。我每次都会收到不同的错误。请帮我理解这个错误以及如何解决它。

编辑: '52.04121.43433.681035' 是因为我输入了 48 小时和 4 家属。此数字因输入而异。这是此场景的完整输出。

Enter the number of hours worked in a week:48
Enter the number of dependents you have:4
Gross pay: $ 867.36
Social Security tax: $ 52.04
Federal income tax: $ 121.43
State income tax: $ 433.68
Union dues: .00
Family health insurance: .00 (additional insurance premiums for your family)
Traceback (most recent call last):
  File "main.py", line 39, in <module>
    netDedFloat = float(netDed)
ValueError: could not convert string to float: '52.04121.43433.681035'

本人初学者,如有不详请见谅

这里的问题是ssfederalstateunionStringfamilyString都是字符串。当你添加字符串时,你连接它们,你不添加它们的数值。

你需要做的就是让这些东西保持浮动。打印时可以使用 format,但不要保留字符串值。

hours = float(input("Enter the number of hours worked in a week:"))
dep = float(input("Enter the number of dependents you have:"))

rate = 16.68
ovrate = 25.02


if hours<=40:
  gross2 = hours * rate
else:
  gross2 = 40*rate + (hours-40)*ovrate
print("Gross pay: ${:.2f}".format(gross2))

ss = gross2*.06
print("Social Security tax: ${:.2f}".format(ss))

federal = gross2*0.14
print("Federal income tax: ${:.2f}".format(federal))

state = gross2*0.5
print("State income tax: ${:.2f}".format(state))

union=10
print("Union dues: .00")

if dep>=3:
  family = 35
  print("Family health insurance: .00 (additional insurance premiums for your family)")
else:
  family = 0

netDed = ss+federal+state+union+family
print("Total deductions: ${:.2f}".format(netDed))

netPay = gross2-netDed
print("Net pay: $",netPay)

输出:

C:\tmp>python x.py
Enter the number of hours worked in a week:123
Enter the number of dependents you have:34
Gross pay: 43.86
Social Security tax: 4.63
Federal income tax: 4.14
State income tax: 71.93
Union dues: .00
Family health insurance: .00 (additional insurance premiums for your family)
Total deductions: 65.70
Net pay: $ 778.1579999999999

您的代码出错,因为在这一行

netDed = ss+federal+state+unionString+familyString 每个变量都是一个字符串,您尝试将字符串添加到字符串。('123.0'+'12'='123.012')

您需要将这些值转换为float。我在更改代码的地方发表评论。

hours = float(input("Enter the number of hours worked in a week:"))
dep = float(input("Enter the number of dependents you have:"))

rate = 16.68
ovrate = 25.02


if hours<=40:
  gross = "{:.2f}".format(float(hours * rate))
else:
  gross = "{:.2f}".format(float((40*rate)+((hours-40)*ovrate)))
gross2 = float(gross)
print("Gross pay: $",gross)

ss = float("{:.2f}".format(gross2*.06)) # Converting in to float
print("Social Security tax: $",ss)

federal = float("{:.2f}".format(gross2*0.14)) # Converting in to float
print("Federal income tax: $",federal)

state = float("{:.2f}".format(gross2*0.5)) # Converting in to float
print("State income tax: $",state)

union=10
print("Union dues: .00")
unionString=float(union) # Converting in to float

if dep>=3:
  family = 35
  print("Family health insurance: .00 (additional insurance premiums for your family)")
else:
  family = 0
familyString = float(family) # Converting in to float

netDed = ss+federal+state+unionString+familyString

netDedFloat = float(netDed)
netDed2 = "{:.2f}".format(netDedFloat)
print("Total deductions: $",netDed2)

netPay = gross2-netDed
print("Net pay: $",netPay)