Newbie
Newbie

Reputation: 163

builtins.TypeError: unsupported operand type(s) for -: 'int' and 'str'

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

Here:

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

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

Output:

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 

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

Hi guys

I was working on a program and this is a part of it. I was trying to print what is shown up.

I keep getting an error builtins.TypeError: object of type 'int' has no len() IT PRINTS EVERYTHING EXCEPT FOR a05 I am trying to keep the colums exactly 20 characters long(i.e. station1, station2 and animal Id). So i put in the condition before print.

I understand that I am calling an unsupported operand for str and int (location shown above) Hoping you guys can help. Thanks :)

UPDATE: It prints: Does not print 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()

Upvotes: 0

Views: 2843

Answers (1)

alecxe
alecxe

Reputation: 473893

The problem is with space and space2 definitions:

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

First it multiplies the length with a space character, which works in python (the string is simply repeated), but after, it tries to evaluate the substraction between an int 20 and the string, which breaks with a TypeError.

You need to enclose 20-len(visit) into parenthesis:

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

Demo:

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

Also, after that space variable is used in:

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

At this point space is of an int type - you need to cast it to string:

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

Upvotes: 1

Related Questions