Lab Assignment #1

Taking a Vacation


Description


Before We Begin
Write a function called answer that takes no arguments and returns the value 42.
Even without arguments, you will still need parentheses.
Don’t forget the colon at the end of the function definition!
4-1


Planning Your Trip
1. Define a function called hotel_cost with one argument nights as input.
2. The hotel costs $140 per night. So, the function hotel_cost should return 140 * nights.
4-2


Getting There
1. Below your existing code, define a function called plane_ride_cost that takes a string, city, as input.
2. The function should return a different price depending on the location, similar to the code example above. Below are the valid destinations and their corresponding round-trip prices.

“Charlotte”: 183
“Tampa”: 220
“Pittsburgh”: 222
“Los Angeles”: 475
4-3


Transportation
1. Below your existing code, define a function called rental_car_cost with an argument called days.
2. Calculate the cost of renting the car:
–Every day you rent the car costs $40.
–if you rent the car for 7 or more days, you get $50 off your total.
–Alternatively (elif), if you rent the car for 3 or more days, you get $20 off your total.
–You cannot get both of the above discounts.
7. Return that cost.

Just like in the example above, this check becomes simpler if you make the 7-day check an if statement and the 3-day check an elif statement.
4-4


Pull it Together
1. Below your existing code, define a function called trip_cost that takes two arguments, city and days.
2. Like the example above, have your function return the sum of calling the rental_car_cost(days), hotel_cost(days), and plane_ride_cost(city) functions.

It is completely valid to call the hotel_cost(nights) function with the variable days. Just like the example above where we call double(n) with the variable a, we pass the value of days to the new function in the argument nights.
4-5


Hey, You Never Know!
1. Modify your trip_cost function definition. Add a third argument, spending_money.
2. Modify what the trip_cost function does. Add the variable spending_money to the sum that it returns.
4-6


Plan Your Trip!
After your previous code, print out the trip_cost( to “Los Angeles” for 5 days with an extra 600 dollars of spending money.
Don’t forget the closing ) after passing in the 3 previous values!
4-7


Code
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):
cost = 40 * days
if days >= 7:
cost = cost – 50
return cost
elif days <= 7 and days >= 3:
cost = cost – 20
return cost
else:
return cost
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)