繁体   English   中英

TypeError:“ float”对象无法解释为整数

[英]TypeError: 'float' object cannot be interpreted as an integer

为什么将对象解释为int而不是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')

从您的电话号码中删除逗号。 逗号被解释为参数分隔符,这意味着使用两个参数而不是一个参数调用round

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

应该

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

问题是您要传递一个浮点数作为round()的第二个参数。 重现问题的非常简单的测试用例-

>>> 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

文档-

舍入(数字[,n位数字])

返回小数点后四舍五入为n位数字的浮点值数字。 如果省略ndigits,则默认为零。

n位数字必须为整数,表示小数点后的位数。

但是你在做-

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

我猜您正在尝试用逗号来表示数字,但这不是Python接受的方式,如果5,156.25就是数字5156.25 ,那么您需要删除逗号。

之所以会出现此错误,是因为您在值中使用了逗号,例如: 5,156.25对于Python,这不是“五千一百五十六十进制二五”。

由于已将其添加到round的调用中,因此逗号后面的部分被添加为第二个参数。

您的电话是:

round(5, 156.25)

这将引发错误。

从值中删除逗号,您应该得到预期的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM