# 01 Before We Begin
def answer():
return 42
# 02 Finding Your Identity
def identity(x):
return x
# 03 Call Me Maybe
def cube(x):
return x ** 3
print (cube(27))
19683
# 04 Function and Control Flow
def is_even(x):
if x % 2 == 0:
return "yep"
else:
return "nope"
# 05 Problem Solvers
import math
def area_of_circle(radius):
return math.pi * (radius ** 2)
# 06 Planning Your Trip
def hotel_cost(nights):
return 140 * nights
# 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
# 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
# 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)
# 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
# 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))
1955
# 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))
779.23
# 13 Gotta Give Me Some Credit
def hotel_cost(nights):
return nights * 140
bill = hotel_cost(5)
# 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))
14.0
# 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) )
14.0 708.75
# 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))
You still owe: 253.125 253.125
# 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))
You still owe: 253.125 253.125
print (1/2)
0.5