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()))
2 Antworten
+ 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)))
+ 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