260,673 views
38 votes
38 votes
Write a program to display MPH (Miles per Hour). Create a function to calculate the MPH. Ask the user for the number of miles driven and the number of minute it took to drive that many miles. Validate that both the number of miles and the number of minutes is a positive, non-zero number. Pass both the number of miles and the number of minutes to the function, and have the function return the MPH.

User Kantal
by
2.8k points

1 Answer

15 votes
15 votes

Answer:

In Python:

def MPH(miles,minutes):

mph = round(60 * miles/minutes,1)

return mph

miles = float(input("Miles: "))

minutes = float(input("Minutes: "))

if miles>0 and minutes>0:

print("MPH: ",MPH(miles,minutes))

else:

print("Positive inputs only")

Step-by-step explanation:

This defines the function

def MPH(miles,minutes):

This calculates mph rounded to 1 decimal place

mph = round(60 * miles/minutes,1)

This returns the calculated mph

return mph

The main begins here

This gets input for miles

miles = float(input("Miles: "))

This gets input for minutes

minutes = float(input("Minutes: "))

If miles and minutes are positive

if miles>0 and minutes>0:

This calls the MPH function

print("MPH: ",MPH(miles,minutes))

If otherwise, this prompts the user for positive inputs

else:

print("Positive inputs only")

User Monalisa
by
3.4k points