DEV Community

PremPrema
PremPrema

Posted on Edited on Fully Autonomous

Day 11: Python While Loop with Real-Life Problems – Micro to Macro

Hi everyone 👋

This is Day 11 of my Python class with Muthu Sir. Yesterday we learned what a while loop is (Day 10 post). Today Sir gave us real-life problems and showed how to turn each one into a loop, step by step.

Sir treats every problem as a story. His words: "Take the story and change it into a program."

He also told us why he asks us to write these blogs: to recall what we learned. And it's true. The variables we learned on Day 1 and Day 2 came back in today's class! 📝


1. Programming Golden Rules 🏆

Sir updated yesterday's ground rules into 9 Golden Rules:

  1. Never say "I don't know." Say "Let me try."
  2. Known to Unknown
  3. Don't think about the entire output
  4. Think about the very next step
  5. Introduce a variable only when it simplifies the solution
  6. Micro to Macro
  7. Dry run before you run
  8. Every program has three parts: Input, Process, Output
  9. Logic first, syntax next

Today's class was all about rule 6: Micro to Macro. First write the small, repeated steps by hand (micro). Then find the pattern and make it a loop (macro).


2. The Three Parts of a While Loop

Sir wrote this with Tamil and English comments:

no = 1              # Initial Value (தொடக்க மதிப்பு)
while no <= 5:      # Condition (கட்டளை)
    print(no, end=' ')
    no = no + 1     # Increment / Decrement
Enter fullscreen mode Exit fullscreen mode

Output: 1 2 3 4 5

Part Question it answers Here
Initial value Where do I start? no = 1
Condition When do I stop? no <= 5
Increment / Decrement How do I move to the next step? no = no + 1

Every problem below uses these same three parts. 👇


3. Problem 1: Five Badges 🎫

A teacher asks five students to stand in a line. Each student wears a badge with the number 1. Display the badge numbers from left to right.

Logic first (in plain words):

  1. Set count to 1.
  2. While count is less than or equal to 5:
    • print 1
    • increase count by 1

Then syntax:

count = 1
while count <= 5:
    print(1, end=' ')
    count = count + 1
Enter fullscreen mode Exit fullscreen mode

Output: 1 1 1 1 1

This is Rule 9: Logic first, syntax next. Sir wrote the algorithm in Tamil and English before writing any code.

Sir also drew it as a flowchart on the board:

   START
     |
 count = 1
     |
 count <= 5 ?  --No-->  STOP
     | Yes
  print 1
     |
 count = count + 1
     |
 (go back to the check)
Enter fullscreen mode Exit fullscreen mode

In the flowchart, the arrow comes down and then goes back up to the check. Sir said this "down and back up" movement is the symbol of a loop. 🔁

💭 Feeling self-doubt? Do this

Sir talked about something many beginners feel. We finish a program quickly, and then we think: "I just remembered this. I don't really have logic."

Sir's answer: when you have that self-doubt, take the program and:

  1. Draw it as a flowchart.
  2. Write it as an algorithm, in Tamil or English.

If you can do that, you will get confidence that you really understand it. Then you can move ahead. 💪


4. Problem 2: Roll Numbers 🔢

Five students enter the classroom one after another. The first gets roll number 1, the second gets 2, and so on. Display the roll numbers.

Micro (by hand):

print(1, end=' ')
print(2, end=' ')
print(3, end=' ')
print(4, end=' ')
print(5, end=' ')
Enter fullscreen mode Exit fullscreen mode

Sir asked us: "Is this a repeated action?" Most of us said yes. 🙋

Then he asked again: "Is it a **100%* repeated action?"* 🤔

No! Each line has a different number (1, 2, 3...). We can make a loop only when the lines are 100% the same.

So how do we make them 100% the same? Introduce a variable (Rule 5):

no = 1
print(no, end=' ')
no = no + 1   # no = 2
print(no, end=' ')
no = no + 1   # no = 3
print(no, end=' ')
Enter fullscreen mode Exit fullscreen mode

Now every pair of lines is 100% the same. 🎯 Sir: whatever is 100% repeated, keep one copy of it and put it inside the loop.

Macro (loop):

no = 1
while no <= 5:
    print(no, end=' ')
    no = no + 1
Enter fullscreen mode Exit fullscreen mode

Output: 1 2 3 4 5

Want 1 to 20? Change only one thing: while no <= 20: 😎


5. Problem 3: Even Positions 🏃

A coach asks students to stand only at even-numbered positions in a queue. Display the first five positions.

Expected: 2 4 6 8 10

Micro:

position = 2
print(position, end=' ')
position = position + 2
print(position, end=' ')
position = position + 2
print(position, end=' ')
Enter fullscreen mode Exit fullscreen mode

Macro:

position = 2
while position <= 10:
    print(position, end=' ')
    position = position + 2
Enter fullscreen mode Exit fullscreen mode

Output: 2 4 6 8 10

💡 The only new thing: we add 2 instead of 1.

Small doubt in class: should we start from 0 or 2? The coach said "even-numbered positions", and positions start from 1. So the first even position is 2.

Sir also said: "Don't skip the micro step, even if you think 'this is boring, I know this.'" Get a good grip first. Then you can do it fast later. ⚡

😅 A mistake from class

One classmate forgot the line position = position + 2. The output was:

2 2 2 2 2 2 2 2 2 ... (never stops!)
Enter fullscreen mode Exit fullscreen mode

The value never changes, so the condition is always true. Infinite loop! Press Ctrl + C to stop it.

Sir's reply: "It is good to make mistakes." Typing and running wrong code teaches us more than only reading. 👍


6. Problem 4: The Special Lift 🛗

A new building has a lift that stops only at every third floor. A person enters at the ground floor and presses "Up" 5 times.

Expected: 3 6 9 12 15

⚠️ Sir warned us: many people start with floor = 3 and while ... directly. That is a mistake. First write it by hand:

print(3, end=' ')
print(6, end=' ')
print(9, end=' ')
print(12, end=' ')
print(15, end=' ')
Enter fullscreen mode Exit fullscreen mode

Then see the repeat, then make the loop.

Way 1: Track the floor

floor = 3
while floor <= 15:
    print(floor, end=' ')
    floor = floor + 3
Enter fullscreen mode Exit fullscreen mode

Sir did a dry run of this on the whiteboard (Rule 7):

floor floor <= 15? Prints
3 ✅ 3
6 ✅ 6
9 ✅ 9
12 ✅ 12
15 ✅ 15
18 ❌ stop

A classmate's doubt: "floor is 3, so why not write while 3 <= 15: directly?"

Sir ran it to show us:

floor = 3
while 3 <= 15:
    print(floor, end=' ')
    floor = floor + 3
Enter fullscreen mode Exit fullscreen mode

Output: 3 6 9 12 15 18 21 24 ... and it never stops! 😱

Why? 3 <= 15 is always true. The number 3 never changes. Only floor changes. So the condition must use the variable, not a fixed number.

Way 2: Track the button presses

Then Sir asked a sharp question: "Where is 15 in the story?" It's not there! The story only says the person pressed the button 5 times. So Sir tracked button:

button = 1
floor = 3
while button <= 5:
    print(floor)
    button = button + 1
    floor = floor + 3
Enter fullscreen mode Exit fullscreen mode

Here we have two variables moving together. button decides when to stop, and floor is what we print.

Way 3: Use multiplication (my classmate's idea 💡)

My classmate Rahul said: "Sir, it's simpler. The button increases, the floor is just button × 3. Why do we need an extra floor variable?"

button = 1
while button <= 5:
    print(button * 3)
    button = button + 1
Enter fullscreen mode Exit fullscreen mode

Floor = button × 3. One variable is enough! (Rule 5: introduce a variable only when it simplifies the solution.)

Sir loved it: "If you can reduce the variables as much as possible, you are a good developer." 🙌

Sir also said this is not the only final solution. When 3 or 4 people discuss, we get many solutions. That's why talking in a live class matters!

🧠 Dry run question (Rule 7)

button = 1
while button <= 5:
    button = button + 1

print(button)
Enter fullscreen mode Exit fullscreen mode

What is printed after the loop?

Answer

6. When button becomes 6, the condition (6 less than or equal to 5) is false, so the loop stops. But button still holds 6.


7. Problem 5: Rocket Countdown 🚀

A rocket launches with a countdown using only even numbers from 10. Display the countdown.

Micro:

count_down = 10
print(count_down)
count_down = count_down - 2
print(count_down)
count_down = count_down - 2
print(count_down)
# ... and so on
Enter fullscreen mode Exit fullscreen mode

This time we subtract (decrement). Before you read Sir's answer, try to fill the three parts yourself:

Part Your answer
Initial value count_down = 10
Condition ?
Decrement ?

👇 Sir's answer

count_down = 10
while count_down >= 0:
    print(count_down, end=' ')
    count_down = count_down - 2
Enter fullscreen mode Exit fullscreen mode

Output: 10 8 6 4 2 0

At 0, the rocket launches! 🚀

💡 Sir: "Till now we always used less than. Now it is greater than or equal to zero." The number is going down, so the condition flips.


8. What I Learned Today

  1. Every while loop has 3 parts: initial value, condition, increment/decrement.
  2. Micro to Macro: first write the repeated steps by hand, then turn them into a loop.
  3. Logic first, syntax next: write the steps in your own words before coding.
  4. Use a variable only when it makes things simpler (lift problem: button * 3).
  5. Dry run before you run: after a loop ends, the variable keeps its last value.

  6. A loop is only for 100% repeated actions. If lines are almost the same, add a variable to make them 100% the same.

  7. Don't jump to the loop. Write it by hand first, even if it feels boring.

  8. Mistakes are good. Infinite loops taught me a lot today! 😅

🏠 Homework (Difficulty: Medium)

In a school sports event, all students wearing odd jersey numbers enter the ground first. After they finish, students with even jersey numbers enter. Display the order up to 10.

I will try this and share my answer in my next post!

💼 Interview question

"What is the difference between for and while?" Sir said that in many other languages they work almost the same. In Python they act a little differently. We will strengthen while first and learn for later.

See you on Day 12! 🐍

Thank you, Muthu Sir! 🙏

Top comments (0)