Abstracting Calculations. We can use functions to hide the complexity of a calculation away from the main part of a program.
You will design a new version of the program you created below. In this new version, you will create a function for the formula used in that program.
Your program shall:
SummaryThe program is to take the number of miles driven and the gallons of gas usedand then calculate the car's miles-per-gallon using the formula:MPG = Miles driven / Gallons of gas used
'''
#variables
total_miles_driven = float(input("Enter the no of miles driven: "))time_driven = float(input("Enter how many hours did it driven: "))gallon_gas = float(input("Enter the gallons of gas used: "))
#Speed equation
speed = float(total_miles_driven) / float(time_driven)
#MPG equation
MPG = float(total_miles_driven) / float(gallon_gas)
#print including speed
print("The milesPerGallon of the car is {MPG}".format(MPG=round(MPG, 2)))print("The speed you were traveling is:", speed)
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here