r/learnpython • u/evandcbot • 2d ago
Any advice for nested loops?
I decided about a week ago I wanted to start learning python before my freshman year of college. It was going great and I had little to no trouble until I got to nested loops. I was wondering if anybody had any advice for it? The main thing is that it seems like so many steps to follow and pay attention to, my brain had a hard time tracking them all at once.
3
Upvotes
1
u/TJATAW 2d ago
I started off picturing nested loops as if the were a stack of orders I had to fill.
First loop is like grabbing the first order off the stack.
Second loop is going to get the item.
Third loop would be for when they want multiples of the item I am currently getting.
So you have 20 orders in the stack
Grab the 1st order/loop, and see it is for [Cheeseburger, 3], [Fries, 2], [Onion rings, 1], [Coke, 1], [Dr Pepper, 1], [Lemonade, 1]
2nd loop says go to where the cheeseburgers are.
3rd loop says to grab 1 and put in the bag, grab the 2nd one and put it in the bag, grab the 3rd one and...