【问题标题】:TypeError: 'float' object cannot be interpreted as an integerTypeError:'float' 对象不能解释为整数
【发布时间】:2015-09-21 11:48:16
【问题描述】:

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

【问题讨论】:

  • 你在哪里得到这个错误?
  • 如果 46,075.25 表示 46075.25,那么您在理解该语言时遇到了严重问题。很少有语言支持数字分隔符,例如 C++14 中的 ' (46'075.25) 和 Verilog 和 Ada 中的 _ (46_075.25)

标签: python if-statement floating-point typeerror


【解决方案1】:

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

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

应该是

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

【讨论】:

    【解决方案2】:

    问题是您将浮点数作为 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
    

    来自documentation -

    round(number[, ndigits])

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

    ndigits 需要为整数,表示小数点后的位数。

    但你在做 -

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

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

    【讨论】:

    • 有没有办法可以保留小数点,或者我应该将其设为 int
    • 5,156.25 是指数字5156.25 吗?
    • 如果是这样,删除所有round()函数调用的逗号。
    • 回答得很好。也可以使用像 int() 或 float() 这样的类型转换。根据要求
    【解决方案3】:

    您收到此错误是因为您在值中使用了逗号,例如此处:5,156.25 - 对于 Python,这不是“十进制五千一百五十六二五”。

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

    那么你的电话是:

    round(5, 156.25)
    

    这会引发错误。

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

    【讨论】:

      猜你喜欢
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 2018-01-19
      相关资源
      最近更新 更多