58.5k views
0 votes
For example, if a car travels at the speed of 40 miles per hour for three 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 the time it has travelled. The program should then express the distance travelled in hourly increments as a list of comma-separated values.

User Pape
by
6.5k points

2 Answers

3 votes
Here's an example Python program that takes input from the user for the speed of a vehicle (in miles per hour) and the time it has travelled, and then outputs the distance travelled in hourly increments as a list of comma-separated values:

python

# get speed and time from user input
speed = int(input("Enter the speed of the vehicle (in mph): "))
time = int(input("Enter the time travelled (in hours): "))

# calculate distance travelled in each hour
distances = []
for t in range(1, time+1):
distance = speed * t
distances.append(distance)

# output distances as a comma-separated list
print("Distances travelled: " + ", ".join(str(d) for d in distances))

# get speed and time from user input
speed = int(input("Enter the speed of the vehicle (in mph): "))
time = int(input("Enter the time travelled (in hours): "))

# calculate distance travelled in each hour
distances = []
for t in range(1, time+1):
distance = speed * t
distances.append(distance)

# output distances as a comma-separated list
print("Distances travelled: " + ", ".join(str(d) for d in distances))
Here's an example of how the program might be run and what the output might look like:

java

# get speed and time from user input
speed = int(input("Enter the speed of the vehicle (in mph): "))
time = int(input("Enter the time travelled (in hours): "))

# calculate distance travelled in each hour
distances = []
for t in range(1, time+1):
distance = speed * t
distances.append(distance)

# output distances as a comma-separated list
print("Distances travelled: " + ", ".join(str(d) for d in distances))

Enter the speed of the vehicle (in mph): 40
Enter the time travelled (in hours): 3
Distances travelled: 40, 80, 120
In this example, the user entered a speed of 40 mph and a time travelled of 3 hours, and the program output a list of distances travelled in hourly increments of 40 miles, 80 miles, and 120 miles.
User Aquaflamingo
by
7.1k points
6 votes

Answer:

Step-by-step explanation:

xd

User Tomtomtom
by
8.2k points