187k views
4 votes
python Distance Traveled 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. Design a program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. It should then use a loop to display the distance the vehicle has traveled for each hour of that time period. Here is an example of the output

User Swissmant
by
5.4k points

1 Answer

3 votes

Answer:

def show_travel(time,speed):

print("Hour\tDistance Travelled\\")

print("-----\t----------\\");

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

res=i*speed

print('%d\t\t%.1f'%(i,res))

def main():

speed=int(input("What is the speed of the vehicle in mph: "))

while speed <= 0:

print("speed must be greater than zero \\")

speed=int(input("What is the speed of the vehicle in mph: "))

time=int(input("How many hours has it traveled: "))

while time <= 0:

print("time must be greater than zero \\")

time=int(input("How many hours has it traveled: "))

show_travel(time,speed)

if __name__== "__main__":

main()

User Jamshed
by
5.0k points