0
def parse_sentence(word_list):

    objects = []

    subj = parse_subject(word_list)
    verb = parse_verb(word_list)
#    objects = [parse_objects(word_list) for word in word_list]
    for word in word_list:
        objects.append(parse_objects(word_list))
#        print(word_list)

    return Sentence(subj, verb, objects)


list = [('stop','the'), ('noun','bear'), ('verb', 'eats'), ('stop','the'),('number', 1), ('adjective','chocolate'), ('error', 'ASDLKDJFLKJ'), ('noun','cake')]
x = parse_sentence(list)
print(x.subject)
print(x.verb)
print(x.objects)

In the above snippet, the for-each loop seems to terminate itself and I'm not sure why. Basically, I take the list below and traverse it pulling out "stops" and "errors".

The output for each of the 3 print tests(subject, verb, objects) should be "Bear", "eats", [1, "chocolate", "cake"]. However, instead I get "Bear", "eats", [1, "chocolate"].

When I change the For-Each loop to a While loop:

while word_list:
    objects.append(parse_objects(word_list))

I get the correct outputs. Why does the For-Each loop fail?

coheedjr
  • 13
  • 5
  • `parse_objects` is removing from `word_list`? If that's the case, see [here](https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list-while-iterating-over-it). – Carcigenicate Feb 01 '22 at 01:15

0 Answers0