DEV Community

Cover image for Understanding Loops, Conditions, and Real-World Logic Through a Train Journey
Kathirvel S
Kathirvel S

Posted on

Understanding Loops, Conditions, and Real-World Logic Through a Train Journey

Train Story Logic with Python

Programming becomes easier when we connect code with real-life stories.
Instead of memorizing syntax, imagine a situation happening in the real world.

In this blog, we are going to learn Python loops and conditions using a train station story.

We will build the logic slowly.

Not directly with code.

First, we will think like humans.

Then we will teach that thinking to Python.

Finally, we will combine everything into a complete working program.


The Story Begins

Imagine there are 300 railway stations.

Two trains are traveling on the same railway route.

  • Train 1 stops at every station divisible by 3
  • Train 2 stops at every station divisible by 8

Now imagine a railway officer asking:

β€œAt which station will both trains stop together?”

This is where logic begins.


Step 1 β€” Think Before Coding

Before touching Python, let us think carefully.

If Train 1 stops at stations divisible by 3:

  • 3
  • 6
  • 9
  • 12
  • 15
  • 18
  • 21
  • 24
  • ...

And Train 2 stops at stations divisible by 8:

  • 8
  • 16
  • 24
  • 32
  • 40
  • ...

Now look carefully.

Which station appears in both lists?

πŸ‘‰ 24

That means:

  • 24 is divisible by 3
  • 24 is divisible by 8

So both trains stop there together.

Now another question appears:

How do we teach Python to discover this automatically?

That is where loops and conditions help us.


Step 2 β€” Understanding the Search Process

Imagine a railway officer checking every station one by one.

The officer starts from station 1.

Then checks:

  • station 2
  • station 3
  • station 4
  • ...

Until station 300.

At every station, the officer asks:

Is this station divisible by 3 AND divisible by 8?
Enter fullscreen mode Exit fullscreen mode

If YES:

Both trains meet here.
Enter fullscreen mode Exit fullscreen mode

This is exactly what our first Python program does.


First Program β€” Finding the First Meeting Station

station_no=1

while station_no<=300:

    if station_no%3==0 and station_no%8==0:
        print(station_no)
        break

    station_no+=1
Enter fullscreen mode Exit fullscreen mode

Line 1

station_no=1
Enter fullscreen mode Exit fullscreen mode

We start checking from station 1.


Line 3

while station_no<=300:
Enter fullscreen mode Exit fullscreen mode

This loop means:

β€œKeep checking stations until station 300.”

Python now behaves like a railway officer checking every station.


Line 5

if station_no%3==0 and station_no%8==0:
Enter fullscreen mode Exit fullscreen mode

This is the heart of the logic.

Let us decode it slowly.


What does % mean?

% is called the modulus operator.

It gives the remainder.

Example:

24 % 3 = 0
Enter fullscreen mode Exit fullscreen mode

Why?

Because 24 divides perfectly by 3.

Similarly:

24 % 8 = 0
Enter fullscreen mode Exit fullscreen mode

So when remainder becomes 0, it means:

The number is divisible.
Enter fullscreen mode Exit fullscreen mode

Understanding AND Condition

station_no%3==0 and station_no%8==0
Enter fullscreen mode Exit fullscreen mode

This means BOTH conditions must be true.

  • divisible by 3 βœ…
  • divisible by 8 βœ…

Only then Python prints the station.


Line 6

print(station_no)
Enter fullscreen mode Exit fullscreen mode

Python prints the station where both trains meet.

Output:

24
Enter fullscreen mode Exit fullscreen mode

Line 7

break
Enter fullscreen mode Exit fullscreen mode

This stops the loop immediately.

Because we only wanted the first meeting station.

Without break, Python would continue checking all 300 stations.


Line 9

station_no+=1
Enter fullscreen mode Exit fullscreen mode

Move to the next station.

Same as:

station_no = station_no + 1
Enter fullscreen mode Exit fullscreen mode

Step 3 β€” Now Let Us Watch Both Trains

Now we want something more interesting.

Instead of finding only the meeting station, let us watch:

  • where Train 1 stops
  • where Train 2 stops
  • where both stop together

This creates a beautiful railway simulation.


Second Program β€” Tracking Both Trains

station_no=1

while station_no<=30:

    if station_no%3==0:
        print("train 1 stopped at :",station_no)

    if station_no%8==0:
        print("train 2 stopped at :",station_no)

    if station_no%3==0 and station_no%8==0:
        print("train 1 and train 2 stopped at",station_no)

    station_no+=1
Enter fullscreen mode Exit fullscreen mode

Understanding the Logic Deeply

Now Python checks every station from 1 to 30.

At each station:

  • Check Train 1
  • Check Train 2
  • Check whether both trains meet

Example Walkthrough


Station 3

3 % 3 == 0
Enter fullscreen mode Exit fullscreen mode

TRUE βœ…

Output:

train 1 stopped at : 3
Enter fullscreen mode Exit fullscreen mode

Station 8

8 % 8 == 0
Enter fullscreen mode Exit fullscreen mode

TRUE

Output:

train 2 stopped at : 8
Enter fullscreen mode Exit fullscreen mode

Station 24

Now something special happens.

24 % 3 == 0
24 % 8 == 0
Enter fullscreen mode Exit fullscreen mode

Both TRUE

Output:

train 1 stopped at : 24
train 2 stopped at : 24
train 1 and train 2 stopped at 24
Enter fullscreen mode Exit fullscreen mode

This is the meeting station.


Step 4 β€” Railway Analytics System

Now let us make the program smarter.

Suppose railway management asks:

  • How many times did the trains meet?
  • Which was the first meeting station?
  • Which was the last meeting station?

Now we move from basic coding to data tracking.

This is where variables become powerful.


Third Program β€” Counting Train Meetings

station_no=1
count=0
first=0
last=0

while station_no<=300:

    if station_no%3==0 and station_no%8==0:

        print("train meets station at :",station_no)

        if count==0:
            first=station_no

        count=count+1
        last=station_no

    station_no=station_no+1

print("total station meet",count)
print("first meet:",first)
print("last meet:",last)
Enter fullscreen mode Exit fullscreen mode

Breaking the Logic Slowly


Variable 1 β€” Count

count=0
Enter fullscreen mode Exit fullscreen mode

This counts how many times trains meet.

Every time trains meet:

count=count+1
Enter fullscreen mode Exit fullscreen mode

Count increases.


Variable 2 β€” First Meeting Station

first=0
Enter fullscreen mode Exit fullscreen mode

Initially unknown.

Now observe this logic:

if count==0:
    first=station_no
Enter fullscreen mode Exit fullscreen mode

This means:

β€œIf this is the first meeting, store the station number.”

Only the first meeting gets stored.


Variable 3 β€” Last Meeting Station

last=0
Enter fullscreen mode Exit fullscreen mode

Every time trains meet:

last=station_no
Enter fullscreen mode Exit fullscreen mode

The value keeps updating.

So finally it stores the latest meeting station.


What Happens Internally?

Meeting stations between 1 and 300 are:

  • 24
  • 48
  • 72
  • 96
  • 120
  • 144
  • 168
  • 192
  • 216
  • 240
  • 264
  • 288

Total = 12 meetings.

So output becomes:

total station meet 12
first meet: 24
last meet: 288
Enter fullscreen mode Exit fullscreen mode

Hidden Mathematics Behind the Story

There is actually a mathematical secret here.

The trains meet at numbers divisible by BOTH 3 and 8.

That means:

LCM of 3 and 8
Enter fullscreen mode Exit fullscreen mode

LCM = 24

So trains meet every 24 stations.

This means:

  • 24
  • 48
  • 72
  • 96
  • ...

This is how programming and mathematics work together beautifully.


Final Thoughts

Most beginners try to memorize Python syntax.

But great programmers think differently.

They first ask:

β€œWhat is the real-world logic?”

Once logic becomes clear, code becomes easy.

Programming is not about typing fast.

Programming is about teaching the computer how to think step by step.

And that is exactly what we did with our train story.


Complete Final Code πŸš†

# Finding first meeting station

station_no=1

while station_no<=300:

    if station_no%3==0 and station_no%8==0:
        print(station_no)
        break

    station_no+=1


# Watching both train stops

station_no=1

while station_no<=30:

    if station_no%3==0:
        print("train 1 stopped at :",station_no)

    if station_no%8==0:
        print("train 2 stopped at :",station_no)

    if station_no%3==0 and station_no%8==0:
        print("train 1 and train 2 stopped at",station_no)

    station_no+=1


# Counting all meetings

station_no=1
count=0
first=0
last=0

while station_no<=300:

    if station_no%3==0 and station_no%8==0:

        print("train meets station at :",station_no)

        if count==0:
            first=station_no

        count=count+1
        last=station_no

    station_no=station_no+1

print("total station meet",count)
print("first meet:",first)
print("last meet:",last)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)