Wednesday, May 29, 2013

Day 7

Dear Friends!
 Below are my day 7 lesson. for Detail Info: https://www.dropbox.com/s/vq7ecyj4jyb7udn/L5-Functions.pptx


"""
Learning Python
Lesson 5 : Functions
Built In Functions

Simple programming exercises from Codecademy
"""

# IGNORE THIS CODE
# *******************************************************************************************************************************************************
maximum = minimum = absolute = type1 = type2 = type3 = type0 = 0
# *******************************************************************************************************************************************************



# Exercise 0 : Beyond Strings
"""
    Check out the code below.
    What do you think it'll do?
    Click Save & Submit Code when you think you have an idea.
"""

def biggest_number(*args):
    print max(args)
    return max(args)
   
def smallest_number(*args):
    print min(args)
    return min(args)

def distance_from_zero(arg):
    print abs(arg)
    return abs(arg)


biggest_number(-10, -5, 5, 10)
smallest_number(-10, -5, 5, 10)
distance_from_zero(-10)



# Exercise 1 : Max
"""
    Create a variable called maximum and set it to the maximum value of
    the following list of numbers: 4,1,6,0,9
    Then print the variable maximum.
"""
maximum = max(4,1,6,0,9)
print maximum




# Exercise 2 : Min
"""
    Create a variable called minimum and set it to the minimum value of
    the following list of numbers: 4,1,6,-5,9
    Then print the variable minimum.
"""
minimum = min(4,1,6,-5,9)
print minimum







# Exercise 3 : Abs
"""
    Create a variable called absolute and set it
    equal to the absolute value of -42 below.
    Then print the variable absolute.
"""
absolute = abs(-42)
print absolute




# Exercise 4 : Type
"""
    Create a variable called type1 and set it to the type of an integer
    Then print type1
   
    Create a variable called type2 and set it to the type of an float
    Then print type2

    Create a variable called type3 and set it to the type of an string
    Then print type3
"""

# Here's an example for you:
type0 = type(True)
print type0

type1 = type(45)
print type1

type2 = type(30.6)
print type2

type3 = type("cat")
print type3







# ************************************************************
# TESTS : DO NOT MODIFY THESE
# ************************************************************
print("")
print("Here are the results of Lesson 5: Functions")
print("Exercise 0 : Test Passed!")
if maximum == 9: print("Exercise 1 : Test Passed!")
else: print("Exercise 1 : Test Failed!")
if minimum == -5: print("Exercise 2 : Test Passed!")
else: print("Exercise 2 : Test Failed!")
if absolute == 42: print("Exercise 3 : Test Passed!")
else: print("Exercise 3 : Test Failed!")
if type1 == type(1) and type2 == type(1.2) and type3 == type("test"): print("Exercise 4 : Test Passed!")
else: print("Exercise 4 : Test Failed!")

def biggest(num1,num2):
    if num1>num2:
       return num1
    elif num2>num1:
        return num2
    else:
        return "They are equal"

print biggest(45, 87)


"""
Learning Python
Lesson 5 : Functions
Importing Modules

Simple programming exercises from Codecademy
"""


# Exercise 0 : Generic Imports
"""
    Type import math
    Print the square root of 25
    For example, print math.sqrt(36) will print 6.0
"""

import math
print math.sqrt(45)

# Exercise 1 : Function Imports
"""
    Change the import so that you only import the sqrt function, like this:
    from math import sqrt
    Print the square root of 100
    For example, print sqrt(81) will print 9.0
"""
from math import sqrt
print sqrt(100)


# Exercise 2 : Function Imports
"""
    Change the import so that you import everything from the math module, like this:
    from math import *
    Print the square root of 144
    For example, print sqrt(25) will print 5.0
"""
from math import*
print sqrt(144)


# ************************************************************
# TESTS : DO NOT MODIFY THESE
# ************************************************************
print("")
"""
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 == "Tokyo":
    return 222
elif city == "New York":
    return 220
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:
        return raw_cost - 20
    elif days >= 7:
        return raw_cost - 50




# 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.
"""





# 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.
"""





# ************************************************************
# TESTS : DO NOT MODIFY THESE
# ************************************************************
print("")
if hotel_cost(1) == 140 : print("Exercise 0 : Test Passed!")
if plane_ride_cost("Paris") == 183 : print("Exercise 1 : Test Passed!")
if rental_car_cost(7) == 230 : print("Exercise 2 : Test Passed!")
if trip_cost("Tokyo", 4, 100) == 1022 : print("Exercise 4 : Test Passed!")

No comments:

Post a Comment