0

Trying to get a single list of word using generator, I get the same list multiple times...Why?

txt = input() def words(): #your code goes here for words in txt: words=(txt.split()) yield words print(list(words()))

23rd Feb 2022, 10:58 AM
Cathyboum
2 Answers
+ 4
Is this what you wanted? If so, I will append an explanation txt = input() def words(sentence): #your code goes here for item in sentence.split(): yield item print(list(words(txt)))
23rd Feb 2022, 11:12 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 4
The very fact that you used the variable name as "words" in the loop indicates that you don't understand what you're trying to do. You need to iterate each word from the text, for this you need to use the split() method: for word in txt.split(): yield word
23rd Feb 2022, 1:39 PM
Solo
Solo - avatar