Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python The two trains problem is a classic algebra problem. In this lab, we will

ID: 3713002 • Letter: P

Question

Python

The two trains problem is a classic algebra problem. In this lab, we will write a program in python to solve every version of this problem.

This problem follows a basic pattern. The values will be replaced by variables in the below question.

Train A, traveling X miles per hour, leaves place1 heading toward place2, total miles away. At the same time Train B, traveling Y mph, leaves place2 heading toward place1. When do the two trains meet? How far are they from each city?

In this lab, you will write a problem that solves this problem. Then you will never have to do it by hand again.

When the meet is determined by

time in hours = (total miles)/(X mph+Y mph)

The distance from place1 is time*X.

The distance from place2 is time*Y.

You may assume that X, Y, and total will be integer values. The results should be given as decimal numbers rounded to 2 digits.

The command round(val, digits) can be used to round.

Your program should look like the below example. The solution to the Dr. Math example is given below.

Explanation / Answer

#Python code:

print "Solve the Two Trains Problem."
first_train_speed = int(raw_input("Enter First Speed: "))
first_place = raw_input("Enter First Place: ")
second_train_speed = int(raw_input("Enter Second Speed: "))
second_place = raw_input("Enter Second Place: ")
total_distance = int(raw_input("Enter total distance: "))

time = total_distance/float(first_train_speed+second_train_speed)
distance_from_place_one = time*first_train_speed
distance_from_place_second = time*second_train_speed


print time,distance_from_place_one,distance_from_place_second
print "Word Problem"
print "Train A, traveling",str(first_train_speed), "miles per hour, leaves", str(first_place), "heading toward" ,str(second_place),"," , str(total_distance),"miles away. At the same time Train B, traveling",str(second_train_speed) , "mph, leaves", str(second_place), "heading toward" , str(first_place), ". When do the two trains meet? How far are they from each city?"
print "Answers"
print "They meet after",str(time),"hours."
print "Train A is",str(distance_from_place_one),"miles from",str(first_place)
print "Train B is", str(distance_from_place_second),"miles from", str(second_place)