python - 类型错误: 'float' 对象无法解释为整数

标签 python if-statement floating-point typeerror

为什么对象被解释为 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)))

关于python - 类型错误: 'float' 对象无法解释为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32687012/

相关文章:

c# - 仅获取 X 长度数字的前 10 个数字

python - 如何从 python 中的 RSA 公钥中提取 N 和 E?

python - 如何将一个函数作为参数传递给另一个函数并在 python 中使用该函数返回的值?

php - 在 Web 服务器上运行带有参数的 python 脚本的最简单方法

javascript - 有没有办法在javascript中执行类似 if (Variable == 1 or Variable == 2 or Variable == 3) 的操作?

ios - swift - if 语句和数组

python - 按组中的第一个值标准化数据

jquery - if() else() 语句问题...:/

math - 为什么十进制数不能用二进制精确表示?

c# - 舍入浮点值