Jerry Cui
Jerry Cui

Reputation: 149

What does it mean by "object of type int has no length" in python?

I tried to write a program in Python 3.6.5 on Windows 10 that will accept integers and then print out the ones that are odd.

This is my code:

def listInput():
    aList = list()
    while True:
        num = input("Please input a number:")
        if num == 'done':
            break
        num = int(num)
        aList.append(num)
    return aList

def isOddNumber(y):
    if y%2 == 0:
        return False
    else:
        return True

def filterOddInList(a):
    i = len(a)
    while i >= 0:
        item = a[(i-1)]
        odd = filterOddInList(item)
        if odd == False:
            x.pop()

def main():
    a = listInput()
    print(a)
    a = filterOddInList(a)
    print(a)

main()

This is what should have happened:

Please input a number:1
Please input a number:2
Please input a number:3
Please input a number:4
Please input a number:5
Please input a number:done
[1, 2, 3, 4, 5]
[1, 3, 5]

This is what actually happened:

Please input a number:1
Please input a number:2
Please input a number:3
Please input a number:4
Please input a number:5
Please input a number:done
[1, 2, 3, 4, 5]
Traceback (most recent call last):
  File "C:\Users\Jerry Cui\Documents\New folder\Homework\oddonly.py", line         30, in <module>
    main()
  File "C:\Users\Jerry Cui\Documents\New folder\Homework\oddonly.py", line 27, in main
    a = filterOddInList(a)
  File "C:\Users\Jerry Cui\Documents\New folder\Homework\oddonly.py", line 20, in filterOddInList
    odd = filterOddInList(item)
  File "C:\Users\Jerry Cui\Documents\New folder\Homework\oddonly.py", line 17, in filterOddInList
    i = len(a)
TypeError: object of type 'int' has no len()

Can someone tell me why this error happened, and what should be the correct code?

Upvotes: 0

Views: 631

Answers (3)

anik120
anik120

Reputation: 23

Few issues with your code:

1) Maybe you meant to call odd = isOddNumber(a[i-1])
After you resolve that issue, you'll run into the following error: "name 'x' is not defined". I am assuming you meant to right x = a at the beginning of the function
After you resolve that issue, you'll run into issues for the statement x.pop(). pop() removes the last item from your list. So if x = [1,2,3,4,5], x.pop() will result in x = [1,2,3,4]. Another x.pop() will result in x = [1,2,3] and so on. What you want is x.remove(item).

To sum up, correct your filterOddInList to:

def filterOddInList(a):
  i = len(a)
  x = a
  while i >= 0:
    item = a[(i-1)]
    odd = isOddNumber(item)
    if odd == False:
        x.remove(item)
    i -= 1
  return x

Upvotes: 0

bashrc
bashrc

Reputation: 380

The problem is in the recursion inside filterOddInList. You are calling it with item which is a number. When you get to len(a), it tries to extract the length of a number and fails.

Upvotes: 2

davecom
davecom

Reputation: 1498

You might want to simplify it a bit. You can combine two of your functions into one-line using a list comprehension. I've rewritten some of your code below.

def list_input():
    a_list = []
    while True:
        num = input("Please input a number:")
        if num == 'done':
            break
        num = int(num)
        a_list.append(num)
    return a_list

def odds_only(list):
    return [x for x in list if x % 2 == 1]

if __name__ == "__main__":
    list = list_input()
    print(list)
    odds = odds_only(list)
    print(odds)

The list comprehension is [x for x in list if x % 2 == 1]. It says: "Let's process each item in list. We will call each item x. If x is odd, put it in the new list."

In other words [(thing that goes in new list) for (item in old list) in (the list) if (include clause)]

Upvotes: 0

Related Questions