Skip to content
Advertisement

Python – len function not working as expected, and giving me the error “TypeError: object of type ‘int’ has no len()”

While studying linked lists from – https://composingprograms.com/pages/23-sequences.html#linked-lists

JavaScript

The program recognizes four as a linked list, But when i plug in x it returns an error instead of returning “False”.

If i change value of x to just [1] or [1,2,3] it returns as expected, but if i enter a normal list [1,2] with 2 values i run into this error. .Why is this?

Advertisement

Answer

Look at the executions, on your condition len(s) == 2 this only satisfies [1, 2] so it checks for the next one is_link(s[1]) which raises an error because it’s a check length of integer in next iteration.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement