DEV Community

Saravanan Cn
Saravanan Cn

Posted on

Python While loop:Learning through Real-world Scenarios

Initially i found it difficult to understand how loops execute,variables changes during each iteration and when loop starts.

So i started solving these problems one by one

1.Frog in a well:

  • A Frog falls into a 60-foot-deep well.
  • During the day it climbs 2 feet.
  • During the night it slips down 0.5 feet.
  • It escapes immediately when it reaches the top.

Python code:

well_depth = 60
frog_position = 0
day = 0

while frog_position < well_depth:
      day = day + 1


    frog_position = frog_position + 2
   if frog_position >= well_depth:
         print("Frog escapes on day:",day)
         break

   frog_position = frog_position - 0.5

print("Total_days:",day)

Output:
Frog escapes on day:40
Total days:40

Enter fullscreen mode Exit fullscreen mode

2. The Saint's Flower Basket

A saint visits 7 temples.At each temples,he offer half of the flowers in his basket.After visiting all 7 temples,he has exactly one flower remaining.We need to calcuate how many flowers he had initially.

Python code:

flowers = 1
temples = 7


while temples>0:
     flowers = flowers*2
     temples = temples - 1
print("Initial Flowers:",,flowers)

Output:
Initial flowers:128
Enter fullscreen mode Exit fullscreen mode

3.TenaliRaman and the Palace Guards

*Tenali Raman receives 1024 lashes from the King.
At each gate,he gives half of his remaining lashes to the guard.
We need to find how many guards receive a share when he has one hash remaining.
*

lashes = 1024
guards = 0

while lashes>1:
      share = lashes/2
      print("Guard receives:",share)

      lashes = lashes - share
      guards = guards + 1

  print("Remaining lashes:",lashes)
  print("Total guards",guards)

Output:
Total guards:10
remaining lashes:1
Enter fullscreen mode Exit fullscreen mode

4.Police and Thief
Problem:
The police and thief start at different positions.

  • police starts at 0 feet
  • thief starts at 30 feet
  • police moves 5 feet in each round.
  • Thief moves 2 feet in each round.

Both move simulatenously until they meet.

Python code:

police = 0
thief = 30

while police != thief:
      police = police + 5
      thief = thief +  2

     print("Police:",police,"Thief:",thief)

print("They meet at:" police)

Output:
Police: 5 Thief: 32
Police: 10 Thief: 34
Police: 15 Thief: 36
Police: 20 Thief: 38
Police: 25 Thief: 40
Police: 30 Thief: 42
Police: 35 Thief: 44
Police: 40 Thief: 46
Police: 45 Thief: 48
Police: 50 Thief: 50

They meet at: 50



Enter fullscreen mode Exit fullscreen mode

5.Two Trains:

Two trains stop at regular intervals.

train 1 stops at every 3rd station.
train 2 stops at every 5th station.

We need to find:
1.All common stations.
2.The first common station.
3.The last common station.

since total number of stations is not specified. i asked the user to enter it.

Python code:

total_stations = int(input("Enter total number of stations: "))

station = 1
last_station = 0

print("Common stations:")

while station <= total_stations:

    if station % 3 == 0 and station % 5 == 0:

        print(station)

        if first_station == 0:
            first_station = station

        last_station = station

    station = station + 1

print("First common station:", first_station)
print("Last common station:", last_station)

Output(for 100 stations):
Common stations:
15
30
45
60
75
90

First common station: 15
Last common station: 90
Enter fullscreen mode Exit fullscreen mode

My key takeaways:
_

  • first we need to understand the problem,identify the variables ,decides the loop condition and how the values change during each iteration.
  • iteration mean one complete execution of code inside a loop. _

Top comments (0)