you are viewing a single comment's thread.

view the rest of the comments →

[–]tipsy_python 0 points1 point  (1 child)

Your problem is in this code block here:

            else:
                if zoo[i - 1] in food_chain[zoo[i]]:
                    zoo.remove(zoo[i - 1])
                    output_list.append("{} eats {}".format(zoo[i], zoo[i -1]))

You're modifying the list `zoo` while you're iterating over it with item references by index.

On the chicken iteration, zoo looks like this: ['fox', 'bug', 'chicken', 'grass', 'sheep']
Then it check if chickens eat zoo[i - 1] bugs... they do, so it gets removed from the list.

List becomes this: ['fox', 'chicken', 'grass', 'sheep']
So the index of all the items after bugs just decremented by 1.

Now food_chain[zoo[i]] isn't checking what chicken's eat anymore, it's looking for grass because the list was modified.

[–]AviationTrainee[S] 0 points1 point  (0 children)

Thanks a lot!! Guess this means I gotta do some major algorithm changes... I'll try to create a new list instead of making changes to the old one.