Friday, May 31, 2013

Completion of Vacation trip.

I learned on how to plan for a trip through programming.

The vacation trip code below:
"""
Learning Python
Lesson 5 : Functions
Taking a Vacation

Simple programming exercises from Codecademy
"""


# Exercise 0 : Planning Your Trip
"""
    First, write a function called hotel_cost that takes the
    variable nights as input. The function should return how
    much you have to pay if the hotel costs 140 dollars for
    every night that you stay.
"""
def hotel_cost(nights):
    return nights * 140

# Exercise 1 : Getting There
"""
    Write a function called plane_ride_cost that takes a
    string, city, as input. The function should return a
    different price depending on the location. Below are
    the valid destinations and their corresponding round-trip prices.

    "Paris": 183
    "New York": 220
    "Tokyo": 222
    "Istanbul": 475
"""
def plane_ride_cost(city):
    if city == "Paris":
        return 183
    elif city == "New York":
        return 220
    elif city == "Tokyo":
        return 222
    else:
        return 475
   
           
# Exercise 2 : Transportation
"""
    Write a function called rental_car_cost that takes days
    as input and returns the cost for renting a car for said
    number of days. The cost must abide by the following conditions:

    Every day you rent the car is $40.

    If you rent the car for 3 or more days, you get $20 off your total.

    If you rent the car for 7 or more days, you get $50 off your total.
    (This does not stack with the 20 dollars you get
    for renting the car over 3 days.)

"""
def rental_car_cost(days):
    raw_cost = days * 40

    if days >= 3 and days < 7 :
        return raw_cost - 20
    elif days >= 7:
        return raw_cost - 50
    else:
        return raw_cost
       
   
# Exercise 3 : Pull it Together
"""
    Now write a function called trip_cost that takes two inputs,
    city and days. city should be the city that you are going to
    visit and days should be the number of days that you are staying.

    Have your function return the sum of the rental_car_cost,
    hotel_cost, and plane_ride_cost functions with their respective inputs.
"""
def trip_cost(city, days, spending_money):
    return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money
       





# Exercise 4 : Expect the Unexpected
"""
    Make it so that your trip_cost function takes a third parameter,
    spending_money. Just modify the trip_cost function to do just as
    it did before, except add the spending money to the total that it
    returns.
"""



# Exercise 5 : Let's go!
"""
    Now that you have it all together, print out the cost of a trip
    to "New York" for 5 days with an extra 200 dollars of spending money.
"""
print trip_cost("New York", 5, 200)

print trip_cost("Istanbul", 10, 100)

print trip_cost("Tokyo", 2, 400)

print trip_cost("Paris", 2, 400)

The answer found below:
Python 2.7.3 (default, Aug  1 2012, 05:16:07)
[GCC 4.6.3] on linux2

1300

Exercise 0 : Test Passed!
Exercise 1 : Test Passed!
Exercise 2 : Test Passed!
Exercise 4 : Test Passed!
>>>
1300
2325

Exercise 0 : Test Passed!
Exercise 1 : Test Passed!
Exercise 2 : Test Passed!
Exercise 4 : Test Passed!
>>>
1300
2325
3122

Exercise 0 : Test Passed!
Exercise 1 : Test Passed!
Exercise 2 : Test Passed!
Exercise 4 : Test Passed!
>>>
1300
2325
3122
943

Exercise 0 : Test Passed!
Exercise 1 : Test Passed!
Exercise 2 : Test Passed!
Exercise 4 : Test Passed!
>>>
1300
2325
982
943

Exercise 0 : Test Passed!
Exercise 1 : Test Passed!
Exercise 2 : Test Passed!
Exercise 4 : Test Passed!

No comments:

Post a Comment