1. Write a Python function, tripCost(distance, vehSpeed, vehMPG, gasCostPerGallo
ID: 3784546 • Letter: 1
Question
1. Write a Python function, tripCost(distance, vehSpeed, vehMPG, gasCostPerGallon, hotelCostPerNight) that returns the cost of a trip of based on several parameters The function's parameters are:
distance: the trip's distance in miles (type: float), vehSpeed: the speed of the vehicle used for the trip, in miles per hour (type: float), vehMPG: the vehicle fuel efficiency in miles per gallon (type: float), gasCostPerGallon: the price of one gallon of gas in dollars (type: float), hotelCostPerNight: the cost of one night in a hotel in dollars (type: float)
Presume that you can only drive 8 hours per day. So, if a trip requires 15.25 hours, it will require a one-night hotel stay at an additional cost (beyond gas costs) equal to hotelCostPerNight. Note: If a trip requires 8.0 hours, it does not require a hotel stay.
Similarly, a trip of 16.0 hours requires one night of hotel, not two.
2. Write a Python function, chooseVehicleForTrip(distance, veh1Name, veh1Speed, veh1MPG, veh2Name, veh2Speed, veh2MPG, gasCostPerGallon, hotelCostPerNight) that computes the cost of a trip for two different vehicles, prints information about the cost, and prints a recommendation which vehicle should be used to save money (note: if trip cost is the same for both vehicles, say something appropriate). The function's parameters are:
distance: the trip's distance in miles (type: float), veh1Name: a string representing the first vehicle (e.g. "Tesla") ,veh1Speed: vehicle 1's speed in miles per hour (type: float), veh1MPG: vehicle 1's fuel efficiency in miles per gallon (type: float), veh2Name: a string representing the second vehicle (e.g. "Dongfeng"), veh2Speed: vehicle 2's speed in miles per hour (type: float), veh2MPG: vehicle 2's fuel efficiency in miles per gallon (type: float), gasCostPerGallon: the price of one gallon of gas in dollars (type: float), hotelCostPerNight: the cost of one night in a hotel in dollars (type: float)
For full credit, chooseVehicleForTrip must use/call tripCost, the function you write for Q1.
Explanation / Answer
1.) function 1:
# your code goes here
def tripCost(distance, vehSpeed, vehMPG, gasCostPerGallon, hotelCostPerNight):
time = vehSpeed/distance
# print(time)
hotel_stay = time//8
if(time%8==0):
hotel_stay = hotel_stay -1
#print(hotel_stay)
hotel_cost = hotel_stay * hotelCostPerNight
#print(hotel_cost)
gallon = distance/vehMPG
gallon_cost = gallon * gasCostPerGallon
#print(gallon_cost)
total_cost = hotel_cost + gallon_cost
print(total_cost)
tripCost(1, 2, 3, 5, 8.0)
Output:
1.6666666666666665
For better clearance, please go through this URL:
http://ideone.com/gpcPIH
Q2.) Code is given as:
# your code goes here
def tripCost(distance, vehSpeed, vehMPG, gasCostPerGallon, hotelCostPerNight):
time = vehSpeed/distance
# print(time)
hotel_stay = time//8
if(time%8==0):
hotel_stay = hotel_stay -1
#print(hotel_stay)
hotel_cost = hotel_stay * hotelCostPerNight
#print(hotel_cost)
gallon = distance/vehMPG
gallon_cost = gallon * gasCostPerGallon
#print(gallon_cost)
total_cost = hotel_cost + gallon_cost
return(total_cost)
def chooseVehicleForTrip(distance, veh1Name, veh1Speed, veh1MPG, veh2Name, veh2Speed, veh2MPG, gasCostPerGallon, hotelCostPerNight):
cost_of_veh1 = tripCost(distance, veh1Speed, veh1MPG, gasCostPerGallon, hotelCostPerNight)
cost_of_veh2 = tripCost(distance, veh2Speed, veh2MPG, gasCostPerGallon, hotelCostPerNight)
if( cost_of_veh1 > cost_of_veh2):
print(veh1Name + ' is the right option to travel')
elif (cost_of_veh2> cost_of_veh1):
print(veh2Name + ' is the right option to travel')
else:
print('You can choose any of '+ veh1Name +', ' + veh2Name)
chooseVehicleForTrip(1, "Maruti", 20, 20, "Ferrari", 30, 10, 200, 800)
chooseVehicleForTrip(2, "Alto", 12, 34, "Indica", 15, 30, 100, 43)
chooseVehicleForTrip(3, "Maruti", 12, 34, "Sumo", 12, 34, 100, 43)
Output:
Ferrari is the right option to travel
Indica is the right option to travel
You can choose any of Maruti, Sumo
For better clarification, please visit this code's URL:
http://ideone.com/8s5e5a
Hope it helps, Do comment your feedback.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.