In: Computer Science
CODE IN PYTHON:
Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip.
User will also enter the number of days it will take to complete the trip. For each night (day-1 nights), user will need a hotel. We will ask for the number of stars for the hotel the user wants to stay. 5 star hotel costs $250 per night. 4 star hotel costs $180 per night. 3 star hotel costs $120 per night, 2 star hotel costs $100 per night and finally 1 star hotel costs $50 per night. Using the hotel costs and the number of nights users will need to stay at a hotel, you can compute the hotel cost of the trip.
Please note that 5 star and 4 star hotels give 10% discount if the user will be staying more than 2 nights -- (these are chain hotels, so even if you are staying at different locations discount apply).
We should also account for the cost of meals. This depends on the type of hotel the user is staying. In general, we take 20% of hotel cost as the meal cost (after 10% hotel discount if applicable).
Example Run #1
Distance: 350
MPG : 15
Gas Price: 3.79
Days Traveling : 3
Hotel Stars (1-5) : 3
Your total cost is $376.43333333333334
Example Run #2
Distance: 400
MPG : 20
Gas Price: 2.99
Days Traveling: 1
Hotel Stars (1-5): 5
Your total cost is $59.800000000000004.
Example Run #3
Distance: 150 MPG : 25 Gas Price: 1.99 Days Traveling : 2 Hotel Stars (1-5): 5 Your total cost is $311.94.
#main.py
def calculate_gas_price(distance, mpg, gas_price): """ function to calculate gas price :param distance: :param mpg: :param gas_price: :return: gas price """ return (distance / mpg) * gas_price def calculate_hotel_and_meal_price(days, hotel_star): """ function to find stay price :param days: :param hotel_star: :return: stay price """ # mapping of star and price hotel_price_map = { 5: 250, 4: 180, 3: 120, 2: 100, 1: 50 } if days <= 1: return 0 hotel_price = hotel_price_map.get(hotel_star, 0) * (days-1) discount = 0 # if 4 or 5 star hotel and stay more than 2 night if (days - 1) > 2 and hotel_star in (5, 4): discount = (hotel_price * 10) / 100 # subtracting discount if any hotel_price -= discount # adding meal price meal_price = (hotel_price * 20) / 100 hotel_price += meal_price return hotel_price def main(): distance = float(input('Distance: ')) mpg = float(input('MPG: ')) gas_price = float(input('Gas Price: ')) days_travelling = int(input('Days Traveling: ')) hotel_star = int(input('Hotel Stars (1-5) :')) # calling function and showing price gas_price = calculate_gas_price(distance, mpg, gas_price) hotel_and_meal_price = calculate_hotel_and_meal_price(days_travelling, hotel_star) print('Your total cost is $' + str(gas_price + hotel_and_meal_price)) if __name__ == '__main__': main()
# OUT
Please do let me know if u have any concern..