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.


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

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

The number changes every line, so let's 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 looks the same. 🎯

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.


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

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

Way 2: Track the button presses

The question talks about pressing the button 5 times, so Sir also 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

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.)

🧠 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

💡 The condition is now >= (greater than or equal), because the number is going down.


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.

At the end, Sir showed the next level: "Difficulty Level: Medium", starting with a school sports event problem. Sir also mentioned the difference between for and while. I will write about those soon. See you on Day 12! 🐍

Thank you, Muthu Sir! 🙏

Top comments (0)