TypeError: object of type 'int' has no len()
len() only measures things that contain items: strings, lists, dicts, sets. You gave it a number, or None.
What it means
A number is a single value with no items, so it has no length. Usually you wanted the number of digits (convert to text first), or the variable holds a count where you expected the collection. The 'NoneType' version means a function returned None.
Common causes
1. Counting the digits of a number
Measure the text form, not the number.
Breaks
digits = len(12345)Works
digits = len(str(12345))2. A count stored where the list was expected
The variable was reused for len() of the list earlier.
Breaks
items = len(cart)
if len(items) > 0:
...Works
count = len(cart)
if count > 0:
...3. A function that returned None
A function with no return statement returns None.
Breaks
def load_rows():
rows = read()
len(load_rows())Works
def load_rows():
rows = read()
return rowsHow to find it in your own code
Print type(x) right before len(). If it is int or float, decide what you meant to measure: digits, or the collection the number came from. If it is NoneType, find the function that produced it and its missing return.
Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.