DEV Community

Sivakumar Mathiyalagan
Sivakumar Mathiyalagan

Posted on

Looping in python-2

Question :
_Two friends are driving separate trains from Chennai to Coimbatore.

Driver 1 drives Train 1, which stops at every 3rd station, while Driver 2 drives Train 2, which stops at every 5th station. There are a total of 30 stops between Chennai and Coimbatore.

Since the two drivers are friends, they want to know at which station they will meet for the first time during their journey.

Write a Python program to determine the first stop where both trains stop together_

Code:

train1 = 3
train2 = 5

totalStops = 30
stopCovered = 1

while stopCovered <= totalStops:

    if stopCovered % train1 == 0 and stopCovered % train2 == 0:
        print(stopCovered)
        break

    stopCovered = stopCovered + 1
Enter fullscreen mode Exit fullscreen mode

Output:

Question :
_A thief snatched a purse and started running at a speed of 2 feet per step. A police officer saw the incident and began chasing the thief at a speed of 5 feet per step. At the beginning of the chase, the thief already had a lead of 40 feet.

Write a Python program to determine how many feet the police officer must cover to catch the thief._

Code:

police = 5
thief = 2

thiefCovered = 40
policeCovered =0

while policeCovered <= thiefCovered:
    policeCovered= policeCovered + police
    thiefCovered = thiefCovered + thief

print(policeCovered)
Enter fullscreen mode Exit fullscreen mode

output:

Top comments (0)