Monday, May 27, 2013

Day 6

Dear Friends!

It was so difficult for me today after a two day missed in class from Grand Bassa county for a family problem. But,  however, I manage to get around the lesson.

Today, we dealt with the topic called functions. Here are the practice works and its
  link: www. dropbox.com/s/vq7ecyj4jyb7udn/L5-Functions.pptx

"""
Learning Python
Lesson 5 : Functions
Function Syntax

Simple programming exercises from Codecademy
"""


# Exercise 0 : Function Junction
"""
    Write a function description as a comment
    Create a function called spam
    The function should print "Eggs!" when it is called.
"""

# Write a function description here and your function below!
#Prints "Eggs!" when called
def spam():
    print "Eggs!"

spam()


# Exercise 1 : Call and Response
"""
    Below is a function called square.
    Call the function and give it the argument 10
"""

# square(n) -> Takes a number and prints its value squared
def square(n):
    """Returns the square of a number."""
    squared = n**2
    print "%d squared is %d." % (n, squared)
    return squared

# Call the function square() below and give it the number 10

square(20)



# Exercise 2 : No one ever does
"""
    Uncomment the function below and look at what it does.
    It should take two arguments, a base and an exponent,
    and raise the first to the power of the second.
    It's currently broken, however, because its parameters are missing.

    Replace the ___ with the parameters base and exponent
    and call power on a base of 37 and a power of 4.
"""


""" <---- DELETE THIS LINE ----->

#def power(___, ___):  # Add your parameters here!
#    result = base**exponent
#    print "%d to the power of %d is %d." % (base, exponent, result)
#power()  # Add your arguments here!

<---- DELETE THIS LINE -----> """



# Exercise 3 : Functions calling functions
"""
        Check out the two functions in the editor:
        one_good_turn and deserves_another.
        The first function adds 1 to number it gets as an argument,
        and the second adds 2.

        In the body of deserves_another, change the function so
        that it always adds 2 to the output of one_good_turn.

        Print the result of calling deserves_another on the number 1.
        What do you think the result will be?
"""

def first_one(n):
    return n + 1
   
def second_one(n):
    return first_one(1) + 2

print second_one(5)


# Exercise 4 : Practice Makes Perfect
"""
        Define a function called cube that takes a number
        and returns the cube of that number.
        (Cubing a number is the same as raising it to the third power).

        Define a second function called by_three that takes one number
        as an argument. If that number is evenly divisible by 3,
        by_three should call cube on that number.
        If the number is not evenly divisible by 3,
        by_three should return False.
"""
# cubes a given number
def cube(x):
    return x**3


# Cube a number if it is divisible by 3, returns false it it not
def by_three(x):
    if x%3== 0:
        return cube(x)
    else:
        return False
   
print by_three(3)
print by_three(7)




# ************************************************************
# TESTS : DO NOT MODIFY THESE
# ************************************************************

Python 2.7.3 (default, Apr 20 2012, 22:44:07)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>>
>>>
>>>
Eggs!
>>>
Eggs!
20 squared is 400.
>>>
Eggs!
20 squared is 400.
4
>>>
Eggs!
20 squared is 400.
4
>>>
Eggs!
20 squared is 400.
4
27
False
>>> 




No comments:

Post a Comment