【问题标题】:builtins.TypeError: unsupported operand type(s) for -: 'int' and 'str'builtins.TypeError: 不支持的操作数类型 -: 'int' 和 'str'
【发布时间】:2014-04-05 07:40:23
【问题描述】:
def printsection1(animals, station1, station2):
    animals=['a01', 'a02', 'a03', 'a04', 'a05']
    station1={'a04': 5, 'a05': 1, 'a03': 62, 'a01': 21}
    station2={'a04': 5, 'a02': 3, 'a03': 4, 'a01': 1}

    print('Number of times each animal visited each station :')
    print('Animal Id'+' '*11+'Station 1'+' '*11+'Station 2'+'           ')

    for name in animals:
        if name in station1:
            visit=str(station1.get(name))
        else:
            visit=0
        if name in station2:
            visit2=str(station2.get(name))
        else:
            visit2=0

这里:

        space=(20-len(visit2))*' '

        print(name+' '*17+str(visit)+space+str(visit2))
    print('='*60)

输出:

Number of times each animal visited each station :
Animal Id           Station 1           Station 2           
a01                 21                  1                  
a02                 0                   3                  
a03                 62                  4                  
a04                 5                   5    
a05                 1                   0 

============================================================

大家好

我正在开发一个程序,这是其中的一部分。我试图打印显示的内容。

我不断收到错误builtins.TypeError: object of type 'int' has no len() 它打印除a05 之外的所有内容 我试图保持列正好 20 个字符长(即 station1、station2 和动物 ID)。所以我在打印之前输入了条件。

我知道我正在调用一个不受支持的 str 和 int 操作数(位置如上所示) 希望各位大神帮忙。 谢谢:)

更新: 它打印: 不打印a05

Number of times each animal visited each station :
Animal Id           Station 1           Station 2           
a01                 21                  1
a02                 0                   3
a03                 62                  4
a04                 5                   5
builtins.TypeError: object of type 'int' has no len()

【问题讨论】:

    标签: python


    【解决方案1】:

    问题在于spacespace2 的定义:

    space=20-len(visit)*' '
    space2=20-len(visit2)*' '
    

    首先它将长度乘以一个空格字符,这在 python 中有效(字符串只是重复),但之后,它尝试评估 int 20 和字符串之间的减法,该字符串以 @987654325 中断@。

    您需要将20-len(visit) 括在括号中:

    space=(20 - len(visit)) * ' '
    space2=(20 - len(visit2)) * ' ' 
    

    演示:

    >>> visit = 'test'
    >>> space=20-len(visit)*' '
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for -: 'int' and 'str'
    
    >>> space=(20-len(visit))*' '
    >>> space
    '                '
    

    另外,space 变量用于:

    print(name+' '*17+str(visit)+space+str(visit2))
    

    此时space 属于int 类型 - 您需要将其转换为字符串:

    print(name+' '*17+str(visit)+str(space)+str(visit2))
    

    【讨论】:

    • 嗨@alecxe 我在发布后但在您回答之前修复了这个问题。我正在编辑我的 qs 以适应新的需求。不过还是谢谢
    • @Newbie:将您的问题编辑成不同的东西被认为是不道德的。这使得原始答案对其他人几乎没有用处。 SO 使用离散的问题/答案格式。
    • @alecxe 正如 DSM 所说,我保留了原始帖子,并进行了更新以向您展示正在发生的事情。很奇怪。
    猜你喜欢
    • 1970-01-01
    • 2018-08-13
    • 2022-07-27
    • 2016-04-15
    • 2012-11-29
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多