27.2k views
10 votes
The distance a vehicle travels can be calculated as follows: distance = speed * time For example: If a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles.Write a program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. The program should use a loop to display the distance the vehicle has traveled for each hour of the time period.

User GodsBoss
by
4.2k points

1 Answer

0 votes

Answer:

In Python:

speed = float(input("Speed: (mile/hr): "))

time = int(input("Time: (hr): "))

for hour in range(1,time+1):

distance = speed * hour

print("Distance: "+str(speed)+"*"+str(hour)+" = "+str(distance))

Step-by-step explanation:

This prompts the user for speed in mile/hr

speed = float(input("Speed: (mile/hr): "))

This prompts the user for time in hr

time = int(input("Time: (hr): "))

This iterates through the input time

for hour in range(1,time+1):

This calculates the distance covered in each hour

distance = speed * hour

This prints the distance covered in each hour

print("Distance: "+str(speed)+"*"+str(hour)+" = "+str(distance))

User Mflowww
by
3.9k points