DEV Community

Cover image for Learn Python Logic Through the Frog Escape Story
Kathirvel S
Kathirvel S

Posted on

Learn Python Logic Through the Frog Escape Story

When we start learning programming, many people jump directly into writing code. But good programmers first try to understand the logic behind the problem.

In this blog, we will solve two interesting problems:

  1. Sum of n numbers
  2. A frog escaping from a 50-feet well

Instead of writing code immediately, we will first think like humans and build the logic step by step.


1. Sum of N Numbers Using While Loop

Suppose we want to find the sum from 1 to n.

Example:

1 + 2 + 3 + 4 + 5 = 15
Enter fullscreen mode Exit fullscreen mode

Step 1: Understand the Logic

Before coding, think carefully.

We need to:

  • Start from number 1
  • Keep adding numbers one by one
  • Stop when we reach n

We will use:

  • one variable for counting numbers
  • one variable for storing the total sum

Step 2: Dry Run

Suppose n = 5

Current Number Total Sum
1 1
2 3
3 6
4 10
5 15

Now the logic becomes easy.


Python Code Using While Loop

n = int(input("Enter a number: "))

i = 1
total = 0

while i <= n:
    total = total + i
    i = i + 1

print("Sum is:", total)
Enter fullscreen mode Exit fullscreen mode

Explanation of the Code

i = 1

We start counting from 1.


total = 0

This variable stores the final sum.

Initially, sum is zero.


while i <= n

The loop runs until i becomes greater than n.


total = total + i

This line adds the current number into the total.

Example:

total = 0 + 1
total = 1 + 2
total = 3 + 3
Enter fullscreen mode Exit fullscreen mode

and so on.


i = i + 1

This increases the value of i by 1 every time.

Without this line, the loop would run forever.


Output

Enter a number: 5
Sum is: 15
Enter fullscreen mode Exit fullscreen mode

2. Frog Story – Escaping From the Well

Now let’s solve a famous logic problem.


The Story

A frog falls into a well that is:

50 feet deep
Enter fullscreen mode Exit fullscreen mode

Every day:

  • Frog climbs up 2 feet
  • At night, it slips down 1.25 feet

Question:

In how many days will the frog escape?


Step 1: Think About the Logic

First understand what happens in one complete day.

During the day:

+2 feet
Enter fullscreen mode Exit fullscreen mode

At night:

-1.25 feet
Enter fullscreen mode Exit fullscreen mode

So actual progress in one full day is:

2 - 1.25 = 0.75 feet
Enter fullscreen mode Exit fullscreen mode

This means the frog slowly moves upward.


Code

feet = 50
up = 2
down = 1.25
day = 0

while feet > 0:
    feet = feet - up + down
    day = day + 1

print(day)
Enter fullscreen mode Exit fullscreen mode

Proper Explanation of This Code

At first glance, this code may look confusing because:

feet starts from 50
Enter fullscreen mode Exit fullscreen mode

Here, feet does not represent how much the frog climbed.

Instead, it represents:

how much distance is still left
Enter fullscreen mode Exit fullscreen mode

Initially:

feet = 50
Enter fullscreen mode Exit fullscreen mode

means:

50 feet are remaining to escape
Enter fullscreen mode Exit fullscreen mode

Understanding the Main Logic

Inside the loop:

feet = feet - up + down
Enter fullscreen mode Exit fullscreen mode

This means:

  • subtract the upward climb
  • add the downward slip

So every day:

remaining distance decreases by 0.75 feet
Enter fullscreen mode Exit fullscreen mode

because:

2 - 1.25 = 0.75
Enter fullscreen mode Exit fullscreen mode

Day-by-Day Understanding

Day 1

Remaining distance:

50 - 2 + 1.25 = 49.25
Enter fullscreen mode Exit fullscreen mode

Day 2

49.25 - 2 + 1.25 = 48.5
Enter fullscreen mode Exit fullscreen mode

The remaining distance keeps decreasing slowly.


day = day + 1

This line counts the number of days.

Each loop means:

one full day completed
Enter fullscreen mode Exit fullscreen mode

while feet > 0

The loop runs until the remaining distance becomes zero or negative.

That means:

frog has escaped
Enter fullscreen mode Exit fullscreen mode

Final Output

67
Enter fullscreen mode Exit fullscreen mode

Important Note About This Logic

This code is mathematically correct according to the loop logic.

But in real-life logic:

  • the frog escapes immediately when it reaches the top
  • it should not slip back on the final day

So the more realistic answer is:

65 days
Enter fullscreen mode Exit fullscreen mode

But the given code treats every day as:

climb first and slip every single time
Enter fullscreen mode Exit fullscreen mode

That is why the output becomes slightly different.


Conclusion

Programming is not only about syntax and coding.
Real programming starts with thinking.

Whether it is finding the sum of numbers or helping a frog escape a well, logic always comes first.

So next time you solve a problem:

Think first, code later.

That is the mindset of a good programmer.

Top comments (1)

Collapse
 
xiaoming_nian_94953c8c9b8 profile image
Andy Nian

The frog escape logic in your example is a neat way to think step-by-step, but the day-by-day math should focus more on practical outcomes, like the exact day the frog reaches the top. Many prep materials skip these details. I've been using prachub.com to find similar practical coding banks, and their system design and DSA resources are surprisingly close to what I've seen in actual interviews. It's better than piecing together threads on Glassdoor.