vendredi 27 janvier 2017

how to get every nth interval of a number in python

For the first if statement I have it checking whether or not you need to stay in a hotel, I noticed that you will need a hotel night for every third interval of 8 hours after the first 8 hours, I need help checking that condition

#Problem #1: This function takes, as arguments, distance, vehSpeed, vehMPG, gasCostPerGallon, and hotelCostPerNight. A hotel night is required for any trip over 8.0 hours, but only one hotel night is required for a 16.0 hour trip.
#it will return the cost of the trip based on the parameters that are entered. All the values for the function should be entered as floats.
def tripCost(distance, vehSpeed, vehMPG, gasCostPerGallon, hotelCostPerNight):
  #check the distance and the speed, time, of the car to find out the costs for gasCostPerGallon and to see if you need a hotel
  #if the time is greater than 8.0 hours you will need a hotel
  time=distance/vehSpeed
  #find the total cost of gas 
  costOfTotalGas=(distance/vehMPG)*gasCostPerGallon
  #it will be intervals for every 3 8's that require multiple nights
  if time > 8.0: #this is for one night in a hotel
     #you will need to stay in a hotel
     #return the total cost of the trip with the hotel cost in it
     totalCost=costOfTotalGas + hotelCostPerNight
     return "The cost of the trip is $"+str(totalCost)
  elif time <= 8.0:
     #you will not need a hotel
     #return the total cost which is just the cost of gas
     return "The cost of the trip is $"+str(costOfTotalGas)

Aucun commentaire:

Enregistrer un commentaire