0
Can I set a for loop back by 1 iteration?
I am trying to do something where items in a list are iterated over by a for loop something like this. Items = [dog, goat, tiger] Now during each cycle of the loop it prints one of the items depending on which one's turn it is now. But there is a scenario where I need to reset the iteration to the previous item so that it goes something like this. 1st line dog 2nd line goat 3rd line tiger 4th line the scenario takes place 5th line goat 6th line tiger And the loop continues as normal. So is there a way I can do this? This would be really useful for me if I can do this.
2 Antworten
+ 2
That looks too specific for a Python for loop.
You can do this with a while loop though:
i = 0
while i<len(yourlist):
#here your code
#access items using i
if special_case:
i -= whatever
i += 1
Depending on the very specific case, there obviously might be other ways.
+ 1
HonFu Thank you so much bro. I didn't know you could do it this way. You gave me a for loop to me once where it used something like this to iterate over each player and there seems to be a situation where resetting it causes some issues with player serialing. But with this Im facing no issues. I can now continue on developing my game at ease. Thanks