[Tutor] Building Starships -- object of type 'int' has no len()

Alan Gauld alan.gauld at btinternet.com
Thu Aug 21 00:56:48 CEST 2014


On 20/08/14 20:06, Terry--gmail wrote:

> We are not quite out of the woods on this last example

> lens = [0] * len(catalog2[0])

This is what happens when you don't test code.
My apologies, the initialisation is all wrong.

Try this instead:

lens = [ [] for n in catalog2[0] ]

That adds an empty list for each element in catalog2's header.

> for row in catalog2:
>      for col, item in enumerate(row):
>          print(col, item, len(item))
>          lens[col].append(len(item))
> lens = [max(col) for col in lens]

This should now work... although I still haven't tested it!



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list