Why im i getting TypeError: object of type 'int' has no len()


#1
def sort(arr):
    while True:
        corrected = False
        for i in range(0, len(arr)- 1):
            if arr[i] > arr[i+1]:
                arr[i], arr[i+1] = arr[i+1], arr[i]
                corrected = True
        if not corrected:
            return arr

print sort([40,31,22,63,84,43,12,73])
print sort([444,322,222,6443,822,434,144,735])
print sort(['dice','carrots','apples','ball','eggs','fruit','grapes'])

userinput = input('use a bunch of numbers: ')

print sort(userinput)

#2

It would appear that when this line executed you may have omitted writing the list inside a list.

[1,2,3,4]

If you typed only,

1, 2, 3, 4

then the user_input being passed to the function is not a list.

Question… Is the instruction to write a function called, sort or was that your choice? I ask because it is never a good idea to create our own functions that re-use names of built-ins or class methods. list.sort() is a method of the ‘list’ class, and one suspects also of the ‘dict’ class. Tuples and sets cannot be sorted, but the other two can be. One does not expect to run across a sort() function call in a typical program.

For my test, I changed the name to arr_sort which nobody will confuse with built-in’s.

============ RESTART: D:/cc/discuss/users/tagace40432/arr_sort.py ============
[12, 22, 31, 40, 43, 63, 73, 84]
[144, 222, 322, 434, 444, 735, 822, 6443]
['apples', 'ball', 'carrots', 'dice', 'eggs', 'fruit', 'grapes']
use a bunch of numbers: 1,2,3,4
Traceback (most recent call last):
  File "D:/cc/discuss/users/tagace40432/arr_sort.py", line 19, in <module>
    print (arr_sort(userinput))
  File "D:/cc/discuss/users/tagace40432/arr_sort.py", line 8, in arr_sort
    arr[i], arr[i+1] = arr[i+1], arr[i]
TypeError: 'str' object does not support item assignment
>>> arr_sort([1,4,3,2])
[1, 2, 3, 4]
>>> print (arr_sort(input('use a bunch of numbers: ').split(',')))
use a bunch of numbers: 4,2,3,1
['1', '2', '3', '4']
>>> 

#3

def arr_sort (arr):
while True:
corrected = False
for i in range(0, len(arr)- 1):
if arr[i] > arr[i+1]:
arr[i], arr[i+1] = arr[i+1], arr[i]
corrected = True
if not corrected:
return arr

print(“Enter as many numbers as you want.\n You can choose between 0 and 9.\nLeave a space between each one”)

userinput = input('use a bunch of numbers: ')

print arr_sort(userinput)

Traceback (most recent call last):
File “/Users/user/PycharmProjects/algurthem/algurthem.py”, line 16, in
print arr_sort(userinput)
File “/Users/user/PycharmProjects/algurthem/algurthem.py”, line 4, in arr_sort
for i in range(0, len(arr)- 1):
TypeError: object of type ‘int’ has no len()


#4

I’m now getting AttributeError: ‘int’ object has no attribute ‘split’


#5

I don’t see anywhere in your code that an int object is created. It must go back to the inputs, somehow. The user input comes back as a string, so can be split on the character specified.


#6

yeah, i want it only int specified not str and for some reason its saying i have to int the users input, cause I’m getting
File “/Users/user/PycharmProjects/algurthem/algurthem.py”, line 14, in
arr_sort(input(‘use a bunch of numbers: ‘).split(’,’))
AttributeError: ‘int’ object has no attribute ‘split’
what could i do set arr_sort == int(arr_sort) but i won’t be able to call arr def? i’ve only been coding for 2 weeks so this is all still new to me


#7

For starters, since you are not doing any math, the user input can be left as a string. To convert it to int will still need a split(), then iterate over the list and convert the strings.

user_input = [int(x) for x in input().split(',')]

#8

getting same error AttributeError: ‘int’ object has no attribute ‘split’


#9

What code are you using to convert to int? I don’t see it.


#10

So in that case,

user_input = [int(x) for x in input('your prompt message').split()]

That will give you a list of integers from a space separated string of numbers.


#11

user_input = [int(x) for x in input().split()]
File “”, line 1
4 3 2 1
^
SyntaxError: invalid syntax


#12

Please post a full version of your code.


#13

def arr_sort (arr):
while True:
corrected = False
for i in range(0, len(arr) - 1):
if arr[i] > arr[i+1]:
arr[i], arr[i+1] = arr[i+1], arr[i]
corrected = True
if not corrected:
return arr

print(“Enter as many numbers as you want.\n You can choose between 0 and 9.\nLeave a space between each one”)
print arr_sort([4,10,2,3,1,5,7,8,9,])
user = [int(x) for x in input().split(’,’)]
print arr_sort(user)

user = [int(x) for x in input().split(’,’)]
File “”, line 1
3 2 1
^
SyntaxError: invalid syntax


#14

Remove the comma from split() so you can use spaces…

>>> user_input = [int(x) for x in input('your prompt message: ').split()]
your prompt message: 9 6 4 8 2 9
>>> user_input
[9, 6, 4, 8, 2, 9]
>>> arr_sort(user_input)
[2, 4, 6, 8, 9, 9]
>>> 

#15

still not working at all, still getting
user = [int(x) for x in input('your prompt message: ').split()]
File “”, line 1
3 2 1
^
SyntaxError: invalid syntax


#16

Is that line of code on line 1? Where is the rest of the code? Where are you testing this code? Do you have an interactive console (shell)?


#17

What python version are you using?

Using input() in python2 is a bad idea, the input is toen validated as code


#18

i use to you python 2 and it would not work cause i was using raw_input i would mix python 2 and 3 so i just use python 3


#19

for my shell I’m using pycharm, Pycharm has it’s own shell i can test the code with


#20

The above is Python 3.

Enter that line at the command prompt of the shell, and after entering, type user and enter to see what echoes to the console.