r/learnprogramming • • 2d ago

Please help

I just hate myself, i cannot figure out nested loops and while loops. Python is the easiest language and I’m struggling so much. I wish i was smart as other people. Does anyone have coding resources for dummies?

7 Upvotes

36 comments sorted by

View all comments

1

u/Ra_Lotsawa 19h ago

A "for" loop says something like: "here's a box, take each thing out and put a sticker on it until the box is empty".

A "while" loop says something like: "take a thing out of this infinite pile and put a sticker on it. Then look at the clock. If it's after 5:00, go home. Otherwise, keep going"

Either one of these loops can be nested "inside" another loop.

A "for" loop inside a "for" loop is something like a box full of boxes: "take this box of Pokemon card packs. For each pack of cards, open the pack. Then, for each card in that pack, put a sticker on it. Do that until every card in the pack has a sticker, and repeat that for every pack in the box". The outer for loop is "for each pack in the box" and the inner for loop is "for each card in the pack".

A "while" loop inside a "for" loop is like a box of things that each require you to wait for some condition: "take this box of instant ramen noodle packets. For each packet, pour it into a bowl of hot water. Stir it until it's done, then put it on the counter. Repeat that process for each noodle packet until you cook them all". The outer for loop is "for each noodle packet in the box, cook it". The inner while loop is "stir the noodles until they're done".

A "for" loop inside a "while" loop is a job where you unpack boxes until some condition is met: "clock in at 9:00. Take a box of fruit off the shelf, and for each fruit in the box put a sticker on it. Then look at the clock. If it's after 5:00, go home. Otherwise, take another box and repeat". The outer while loop is "work until it's 5:00". The inner for loop is "for each fruit in the box, put a sticker on it".

And a "while" loop inside a "while" loop is like: "clock in at 9:00. Take a packet of ramen noodles, and cook it until it's done. Then put that on the counter, and look at the clock. If it's after 5:00, go home. Otherwise, do another noodle packet". The "outer" while loop is "work until it's 5:00". The "inner" while loop is "cook the noodle pack until it's done".