The distance a vehicle travels can be calculated as follows: distance = speed *
ID: 3814674 • Letter: T
Question
The distance a vehicle travels can be
calculated as follows:
distance = speed * time.
For example, if a train travels 40 miles per hour for three hours, the distance traveled is 120 miles. Write a program using python that asks the user for the speed of a vehicle in mph and the number of hours it has traveled. It should then use a while loop to display
the distance the vehicle has traveled for each hour of that time period.
Be sure to use input, format, and while loops correctly. When you run it, this should be the result.
Enter the speed of the vehicle in mph:
Enter the number of hours traveled:
Hours. Distance Traveled
-----------------------------------------------------------
Explanation / Answer
Python code:-
speed = input('Enter the speed in mph: ')
print (speed)
hours = int(input('How many hours traveled? '))
print (hours)
print
msg = 'Hours Distance Traveled'
print msg
print '-----------------------------------------------'
#Calculate Distance Traveled
for time in range(1, 1 + hours):
DistanceTraveled = speed * time
print time, ' ' ,DistanceTraveled, 'miles'
#End
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.