TypeError: 'float' object cannot be interpreted as an integer

不问归期 提交于 2020-01-16 15:44:22

问题


Why is the object being interpreted as a int and not a float.

main2 = True

while main2:
     try:
        amount = float(input('annual gross income: '))
        namount = float(amount)
        expenses = float(input('annual expenses: '))
        nnexpenses = float(expenses)



        if(9226 <= namount <= 37450):
                print('Your tax rate is  $922.50 + 15%')
                print(float(round(namount - namount*0.15 - 922.50 - nnexpenses)))
        if(namount <= 9225):
                print('Your tax rate is 10%')
                print(float(round(namount - namount*0.10 - nnexpenses,2)))
        if(37451 <= namount <= 90750 ):
                print('Your tax rate is  $5, 156.25 + 25%')
                print(float(round(amount - namount*0.25 - 5,156.25 - nnexpenses)))
        if(90751 <= namount <= 189300):
                 print('Your tax rate is  $18,481.25 + 28%')
                 print(float(round(amount - namount*0.28 - 18,481.25 - nnexpenses))) 
        if(189301 <= namount <= 411500):
                print('Your tax rate is  $46,075.25 + 33%')
                print(float(round(namount - namount*0.33 - 46,075.25 - nnexpenses)))
        if(411501 <= namount <= 413200):
                 print('Your tax rate is  $119,401.25 + 35%')
                 print(float(round(namount - namount*0.35 - 119,401.25 - nnexpenses)))
        if(413201 <= namount):
                 print('Your tax rate is  $119,996.25 + 39.6%')
                 print(float(round(namount - namount*0.396 - 119,996.25 - nnexpenses)))

        #print('Annual Net Income: ', round(result,2))
     except(ValueError,NameError):
         #if(ValueError):
         print('Please enter a number and postive balance.')
         #else:
             #print('Get out of debt')

回答1:


Remove the commas from your numbers. The commas are being interpreted as argument separators, meaning round is being called with two arguments instead of one.

print(float(round(namount - namount*0.35 - 119,401.25 - nnexpenses)))

should be

print(float(round(namount - namount*0.35 - 119401.25 - nnexpenses)))



回答2:


The issue is that you are passing a floating point number as the second argument for round() . A very simple testcase to reproduce the issue -

>>> round(1.5,1.5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer

From documentation -

round(number[, ndigits])

Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero.

The ndigits needs to be an integer, it signifies the number of digits after the decimal point.

But you are doing -

print(float(round(amount - namount*0.25 - 5,156.25 - nnexpenses)))

I am guessing you are trying to represent numbers with comma, but that is not how Python accepts it, if 5,156.25 is meant to be the number 5156.25 , then you need to remove the comma.




回答3:


You are getting this error because you are using a comma in your values, for example here: 5,156.25 - for Python, this isn't "five thousand and one hundred and fifty six decimal two five".

Since you have added it to your call to round, the part after the comma gets added as second argument.

Your call is then:

round(5, 156.25)

Which will raise an error.

Remove the commas from your values, and you should get the results you expect.



来源:https://stackoverflow.com/questions/32687012/typeerror-float-object-cannot-be-interpreted-as-an-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!