這是我的代碼(它有點長,可能會被簡化)
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)
當我運行它時,我收到以下訊息:
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: $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'
我是初學者,如果我要求您詳細說明,請原諒我。
uj5u.com熱心網友回復:
您的代碼給出了一個錯誤,因為在這一行
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: $10.00")
unionString=float(union) # Converting in to float
if dep>=3:
family = 35
print("Family health insurance: $35.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)
uj5u.com熱心網友回復:
這里的問題是 , ss, federal,state和unionString都是familyString字串。當您添加字串時,您將它們連接起來,而不是添加它們的數值。
你需要做的是將這些東西保持為浮動。您可以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: $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)
輸出:
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: $2743.86
Social Security tax: $164.63
Federal income tax: $384.14
State income tax: $1371.93
Union dues: $10.00
Family health insurance: $35.00 (additional insurance premiums for your family)
Total deductions: $1965.70
Net pay: $ 778.1579999999999
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/467302.html
