The first step in this assignment was to create a function called answer, that takes no arguments, and returns the value of 42. In order to do this, I copied the format of the previous functions. Typed out, the function is as follows:
“def answer():
return 42”
The second step, was to define another function, called hotel_cost, which takes the argument nights, and basically communicates that a stay at a hotel would cost $140 per night. It looks like this:
“def hotel_cost(nights):
return 140 * nights”
The third step, which was the most confusing, instructed you to first define a function called “plane_ride_cost” that takes the argument “city” and returns different prices, depending on the city, afterwards you needed to create if statements which named the different cities and how much they would cost. The code was as follows:
“def hotel_cost(nights):
return 140 * nights
def plane_ride_cost(city):
if city ==”Charlotte”:
return 183
elif city ==”Tampa”:
return 220
elif city ==”Pittsburgh”:
return 222
elif city ==”Los Angeles”:
return 475″
In the next step, we would need to define a function called rental_car_cost with an argument called days. Then calculate the cost with the following conditions:
Every day you rent the car costs $40.
If you rent the car for 7 or more days, you get $50 off your total.
if you rent the car for 3 or more days, you get $20 off your total.
You cannot get both of the above discounts.
Afterwards we would return the cost. The code is as follows:
def hotel_cost (nights):
return 140 * nights
def plane_ride_cost (city):
if city == “Charlotte”:
return 183
elif city == “Tampa”:
return 220
elif city == “Pittsburgh”:
return 222
elif city == “Los Angeles”:
return 475
def rental_car_cost (days):
if days < 3:
return 40 * days
elif days > 6:
return 40 * days – 50
else:
return 40 * days – 20
The next step would essentially help us determine how much the total cost of our trip would add up to. We would need to define a function called trip_cost that takes two arguments, city and days, the function would then return the sum of calling the rental_car_cost(days)
, hotel_cost(days)
, and plane_ride_cost(city)
functions.
The code is as follows:
def hotel_cost (nights):
return 140 * nights
def plane_ride_cost (city):
if city == “Charlotte”:
return 183
elif city == “Tampa”:
return 220
elif city == “Pittsburgh”:
return 222
elif city == “Los Angeles”:
return 475
def rental_car_cost (days):
if days < 3:
return 40 * days
elif days > 6:
return 40 * days – 50
else:
return 40 * days – 20
def trip_cost (city, days):
return rental_car_cost(days) + hotel_cost (days) + plane_ride_cost (city)
In the next step, we’d simply be changing the function called trip_cost by adding a third argument called spending_money. The next thing would be to add the variable spending_money to the sum it returns. The code is as follows:
def hotel_cost (nights):
return 140 * nights
def plane_ride_cost (city):
if city == “Charlotte”:
return 183
elif city == “Tampa”:
return 220
elif city == “Pittsburgh”:
return 222
elif city == “Los Angeles”:
return 475
def rental_car_cost (days):
if days < 3:
return 40 * days
elif days > 6:
return 40 * days – 50
else:
return 40 * days – 20
The final step was to simply after the code from the last step, print the trip_cost(
to "Los Angeles"
for 5 days with an extra 600 dollars of spending money. The code is as follows:
def hotel_cost (nights):
return 140 * nights
def plane_ride_cost (city):
if city == “Charlotte”:
return 183
elif city == “Tampa”:
return 220
elif city == “Pittsburgh”:
return 222
elif city == “Los Angeles”:
return 475
def rental_car_cost (days):
if days < 3:
return 40 * days
elif days > 6:
return 40 * days – 50
else:
return 40 * days – 20
def trip_cost (city, days, spending_money):
return rental_car_cost(days) + hotel_cost (days) + plane_ride_cost (city) + spending_money
print trip_cost (“Los Angeles”, 5, 600)