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.
3 Réponses
+ 2
zcrhy
Why there is sum in 2nd case if you are already getting length of word?
+ 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))
0
Thank you very much! That makes sense! The first being a list comprehension and the second trying to sum an individual int value.