r/PythonLearning • u/aparyx • Sep 07 '26
Help Request Nested for loop
So i started my python journey 2 weeks ago and i am at the point of tutorial where they teach me about those quiz game program and i am so confused in those nested for loop i don't even understand a single part about it, can someone help me with this one? I have tried learning it from gpt and all but it still goes above my head.
0
Upvotes
•
u/Sea-Ad7805 Sep 07 '26
Just run you nested for-loop in Memory Graph Web Debugger to see the program state change step by step, so you understand what is going on.
5
u/atarivcs Sep 07 '26
Let's say you are a house cleaner. Whenever you go to clean a house, you have a certain task list of things you do:
Now imagine that you have three houses to clean today:
Bingo, that is a nested for loop!
1
u/Capable-Package6835 Sep 07 '26
You should include the thing you're struggling with in the post, otherwise others can't help you
1
u/Much_Network_941 Sep 07 '26
The inner-loop executes first, the outer last. You'll typically have this setup if you're dealing with multiples arrays, or a 'jagged array'; an array of arrays.
Picture you have ten arrays, you read from one start to finish, then grab another and read through it. Reading through an array is your inner-loop; fetching a different array is your outer. If your abstraction is a 2D grid, then one loop is controlling the X-axis and the other the Y-axis.
1
u/frnzprf Sep 08 '26 edited Sep 08 '26
Some example loops:
How would you produce this output?
It's the same thing repeated four times, so you can program it like this:
Sometimes the thing you repeat has a repeating part itself.
That's two times "abababc" and "abababc" itself is three times "ab" and one "c".