Average Word Length ('int' object is not iterable) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Average Word Length ('int' object is not iterable)

Hello! What is the difference between text = input() words = text.split() print(sum(len(word) for word in words)) AND text = input() words = text.split() for word in words: print(sum(len(word))) The first one works, while the second returns int object is not iterable.

28th May 2021, 5:01 PM
zcrhy
3 Answers
+ 2
zcrhy Why there is sum in 2nd case if you are already getting length of word?
28th May 2021, 5:27 PM
A͢J
A͢J - avatar
+ 2
zcrhy In 1st case you are making the list of length of word then getting sum of that list In 2nd you are getting length of word then doing sum of integer value that's why there is error sum function works for list not for single int value. see here 2nd will give error: print (sum([3, 7])) print (sum(7))
28th May 2021, 5:30 PM
A͢J
A͢J - avatar
0
Thank you very much! That makes sense! The first being a list comprehension and the second trying to sum an individual int value.
29th May 2021, 12:52 PM
zcrhy