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

Time:04-29

Here is my code (it is kind of long and could probably be simplified)

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: $10.00")
unionString=str(union)

if dep>=3:
  family = 35
  print("Family health insurance: $35.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)

When I run it, I get the following message:

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'

I have tried everything I have read online. I get a different error every time. Please help me understand this error and how to solve it.

Edit: '52.04121.43433.681035' is because I entered 48 for hours and 4 for dependents. This number varies depending on the inputs. This is the complete output for this scenario.

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: $10.00
Family health insurance: $35.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'

CodePudding user response:

The problem here is that ss, federal, state, unionString and familyString are all strings. When you add strings, you concatenate them, you don't add their numerical values.

What you need to do is keep those things as floats. You can use format when you print them, but don't keep the string values.

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 = 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: $10.00")

if dep>=3:
  family = 35
  print("Family health insurance: $35.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)

CodePudding user response:

Your code gives an error because in this line

netDed = ss federal state unionString familyString every variable is a string and you try to add string to string.('123.0' '12'='123.012')

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))
print("Social Security tax: $",ss)

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

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

union=10
print("Union dues: $10.00")
unionString=float(union)

if dep>=3:
  family = 35
  print("Family health insurance: $35.00 (additional insurance premiums for your family)")
else:
  family = 0
familyString = float(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)
  • Related