Hi everyone 👋
This is Day 12 of my Python class with Muthu Sir (Day 11 post). Today was all stories: pocket money, a frog stuck in a well, and a saint with a flower basket. Every story became a while loop.
1. Sir's Warning: Practice! 🏏
Sir started the class with a strong message. Many of us had not done yesterday's homework.
He said: "Failure in programming starts from the moment you stop practising." Even great cricketers like Sachin Tendulkar and Virat Kohli practise every day. We are normal people, so we need practice even more. Try every task for at least 5 to 10 minutes.
About yesterday's homework (odd and even jersey numbers): most of us used two loops. Sir actually wanted us to try it with a single loop. My classmate Vasanth tried that. Sir's point: "At least try one level." 💪
2. Story 1: The Daughter's Vacation 💰
A daughter is going on a 5-day vacation. She asks her father for Rs. 5 every day to save. Her father suggests a different plan: Day 1: Rs. 1, Day 2: Rs. 2 ... Day 5: Rs. 5. She agrees.
Find the total for both plans. Then redo it for 10 days.
Plan A: Rs. 5 every day
Sir said: imagine the daughter has a box. Right now there is nothing in it. Mathematically, "nothing" is 0.
Micro:
box = 0
day = 1
box = box + 5
day = 2
box = box + 5
day = 3
box = box + 5
day = 4
box = box + 5
day = 5
box = box + 5
print(box) # 25
Sir asked: "Is this a repeated action?" The box lines are repeated, but day = 2, day = 3, day = 4... are not. So change them to the same line: day = day + 1.
Now both lines repeat 100%. Keep one copy, delete the others, and put while in front:
box = 0
day = 1
while day <= 5:
box = box + 5
day = day + 1
print(box)
Output: 25
Plan B: Dad's plan (1, 2, 3, 4, 5)
Day 1 → add 1, Day 2 → add 2, Day 3 → add 3... Did you notice? The money added is the same as the day number! So instead of box + 1, box + 2... write box + day:
box = 0
day = 1
while day <= 5:
box = box + day
day = day + 1
print(box)
Output: 15
The twist: 10 days 🤯
Sir joked that the father was trying to cheat his daughter. For 5 days she gets only 15 instead of 25!
But the story's last line says: "Redo the same activity for 10 days." Change 5 to 10 in both loops:
| Days | Daughter's plan (Rs. 5 daily) | Father's plan (1, 2, 3...) | Winner |
|---|---|---|---|
| 5 | 25 | 15 | Daughter's plan |
| 10 | 50 | 55 | Father's plan! 🎉 |
Same loop. Only one number changed. And the answer flipped!
3. One Program → Many Programs ➕✖️
Addition of first n numbers
Sir showed that Dad's plan is actually a famous program: 1 + 2 + 3 + ... + n. He only changed the variable names:
total = 0
no = 1
while no <= 10:
total = total + no
no = no + 1
print(total) # 55
Multiplication of first n numbers
Then Sir asked: can you do 1 × 2 × 3 × ... × n? Classmates got 120 for 5 numbers and 3628800 for 10. ✅
But there is one trap. 🪤
total = 1 # NOT 0!
no = 1
while no <= 10:
total = total * no
no = no + 1
print(total) # 3628800
Why total = 1? If we start with 0: 0 × 1 = 0, 0 × 2 = 0... the answer is always 0!
| Operation | Start with | Name | Tamil |
|---|---|---|---|
| Addition | 0 | Additive Identity | கூட்டல் சமனி |
| Multiplication | 1 | Multiplicative Identity | பெருக்கல் சமனி |
Sir said we derived 2–3 new programs from one story. 🌱
4. Story 2: The Frog in the Well 🐸
A frog falls into a 60-foot well. Every day it climbs 2 feet. Every night it slips down 0.5 feet. Once it reaches the top during the day, it escapes and does not slip back. How many days does it take?
Sir said this is the kind of scenario question you get in interviews. And: "If you understand the story's logic, converting it into a program is just simple syntax."
Micro (day by day)
total_depth = 60
day = 1
total_depth = total_depth - 2 # 58 (climbs in the day)
total_depth = total_depth + 0.5 # 58.5 (slips at night)
day = 2
total_depth = total_depth - 2 # 56.5
total_depth = total_depth + 0.5 # 57
How long do we repeat this? We don't know! But we know when to stop: when the frog is close enough to the top to climb out in one day (2 feet or less).
Sir's loop
total_depth = 60
day = 0
while total_depth >= 2:
total_depth = total_depth - 2
total_depth = total_depth + 0.5
day = day + 1
print(day)
Output: 39
🤔 My doubt: 39 or 40?
I did a dry run (Rule 7!) and printed total_depth after the loop. It was 1.5. So after 39 days the frog is still 1.5 feet below the top. It climbs out on the next morning, day 40.
So 39 = full days (climb + slip) before the last jump, and 40 = the day it escapes. In class, Sir said "39, 40, 41 is OK, if you understand" the logic. The idea of the loop is the same. 🙂
Here is a version that counts the escape day directly:
height = 0
day = 0
while True:
day = day + 1
height = height + 2 # climbs in the day
if height >= 60:
break # escaped! no slipping tonight
height = height - 0.5 # slips at night
print(day) # 40
(We haven't learned while True and break in class yet. Sir said we will discuss "always true" later.)
5. Story 3: The Saint's Flower Basket 🌸
A saint plucks flowers on his morning walk. On the way home, he visits 7 temples. At each temple, he offers half of the flowers in his basket. He reaches home with exactly 1 flower. How many flowers did he start with?
Hint: is it easier to start from the beginning or work backwards?
We don't know the start. But we know the end: 1 flower. So go backwards! (Rule 2: Known to Unknown.)
- Leaving the 7th temple: 1 flower
- Entering the 7th temple: 2 (he gave 1, kept 1)
- Entering the 6th temple: 4
- Entering the 5th temple: 8
- ... keep doubling ...
- Entering the 1st temple: 128 🌸
Sir also checked it forwards: 128 → gives 64 at the 1st temple → 64 left → gives 32 at the 2nd → ... → 1 left. ✅
Micro:
count = 1
temples_visited = 7
count = count * 2 # 2
temples_visited = temples_visited - 1
count = count * 2 # 4
temples_visited = temples_visited - 1
count = count * 2 # 8
Macro:
count = 1
temples_visited = 7
while temples_visited > 0:
count = count * 2
temples_visited = temples_visited - 1
print(count)
Output: 128 (which is 2 to the power of 7)
💬 "Average person is your strength"
This was my favourite part of the class. Sir warned again: "Don't write while first. That is the danger." First solve the story, then let your mind feel the repeat (7, 6, 5, 4, 3, 2, 1...). There is a lot of work behind one loop.
Some people learned this in school or college, or from a brother or sister, so it comes easily for them. Sir said: "I see this for the first time. I am an average person. That is very convenient. **The average person is your strength." 🙏
6. What I Learned Today
- Practice every day. Even great players do.
- If a line is not repeated (
day = 2,day = 3), change it into a repeated one (day = day + 1). - Look for patterns: in Dad's plan, the money equals the day, so
box = box + day. - Start addition with 0, multiplication with 1.
- When you know the end but not the start, work backwards.
- Always dry run: printing the variable after the loop helped me understand the frog problem.
🏠 Homework
Sir shared two new stories: Tenali Raman and the guards (the reverse of the saint's story!) and two trains stopping at every 3rd and 5th station. Sir said: "I want your attempt, not the output."
Sir also asked us to draw the flowchart, write the algorithm, write a blog and keep a digital presence. So here I am! 😄
See you on Day 13! 🐍
Thank you, Muthu Sir! 🙏
Top comments (0)