#!/usr/bin/env python # coding: utf-8 # # Chapter 08 Taking a Vacation # # In[1]: # 01 Before We Begin def answer(): return 42 # In[2]: # 02 Finding Your Identity def identity(x): return x # In[3]: # 03 Call Me Maybe def cube(x): return x ** 3 print (cube(27)) # In[4]: # 04 Function and Control Flow def is_even(x): if x % 2 == 0: return "yep" else: return "nope" # In[5]: # 05 Problem Solvers import math def area_of_circle(radius): return math.pi * (radius ** 2) # In[6]: # 06 Planning Your Trip def hotel_cost(nights): return 140 * nights # In[7]: # 07 Getting There 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 else: return 0 # In[8]: # 08 Transportation 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 else: return 0 def rental_car_cost(days): cost = days * 40 if days >= 7: cost = cost - 50 elif days >= 3: cost = cost - 20 return cost # In[9]: # 09 Pull it Together 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 else: return 0 def rental_car_cost(days): cost = days * 40 if days >= 7: cost = cost - 50 elif days >= 3: cost = cost - 20 return cost def trip_cost(city, days): return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) # In[10]: # 10 Hey You Never Know 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 else: return 0 def rental_car_cost(days): cost = days * 40 if days >= 7: cost = cost - 50 elif days >= 3: cost = cost - 20 return cost def trip_cost(city, days, spending_money): return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money # In[11]: # 11 Plan Your Trip 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 else: return 0 def rental_car_cost(days): cost = days * 40 if days >= 7: cost = cost - 50 elif days >= 3: cost = cost - 20 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)) # In[12]: # 12 Coming Back Home def hotel_cost(nights): return nights * 140 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 = days * 40 if days >= 7: cost = cost - 50 elif days >= 3: cost = cost - 20 return cost def trip_cost(city, days, spending_money): return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money # You were planning on taking a trip to LA # for five days with $600 of spending money. print (2734.23 - trip_cost("Los Angeles", 5, 600)) # In[13]: # 13 Gotta Give Me Some Credit def hotel_cost(nights): return nights * 140 bill = hotel_cost(5) # In[14]: # 14 At a Bare Minimum def hotel_cost(nights): return nights * 140 bill = hotel_cost(5) def get_min(balance, rate): return balance * rate print (get_min(bill, 0.02)) # In[15]: # 15 Something of Interest def hotel_cost(nights): return nights * 140 def get_min(balance, rate): return balance * rate def add_monthly_interest(balance): return balance * 1.0125 bill = hotel_cost(5) print (get_min(bill, 0.02)) print( add_monthly_interest(bill) ) # In[16]: # 16 Paying Up def hotel_cost(nights): return nights * 140 bill = hotel_cost(5) def add_monthly_interest(balance): return balance * (1 + (0.15 / 12)) def make_payment(payment, balance): balance = add_monthly_interest(balance - payment) print ("You still owe:", balance) return balance print (make_payment(bill / 2 + 100, bill)) # In[17]: # 17 Run It def hotel_cost(nights): return nights * 140 bill = hotel_cost(5) def add_monthly_interest(balance): return balance * (1 + (0.15 / 12)) def make_payment(payment, balance): balance = add_monthly_interest(balance - payment) print ("You still owe:", balance) return balance print (make_payment(bill / 2 + 100, bill)) # In[18]: print (1/2)