Monday, May 27, 2013

FUNCTIONS LESSON

Introduction to Functions
Υ
We do two things with functions:
Declare
Functions
def add_one(x):
return x + 1
"""
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!
#Print "Egg!" 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(50)




# 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(z):
    return z + 1
  
def second_one(n):
    return first_one(5) + 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.
"""
#Cube a given number!

def by_three(n):
    if n%3 == 0:
        return cube(n)
  
    else:
        return False
  
print by_three(3)
print by_three(7)

Python 2.7.3 (default, Aug  1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>>
Eggs !
>>>
Eggs !
50 squared is 2500.
>>>
Eggs !
50 squared is 2500.
8
>>>
Eggs !
50 squared is 2500.
8
5
>>>
Eggs !
50 squared is 2500.
8
125
>>>
Eggs !
50 squared is 2500.
8
125
>>>
Eggs !
50 squared is 2500.
8
>>>
Eggs !
50 squared is 2500.
8
>>>
Eggs !
50 squared is 2500.
8
>>>
Eggs !
50 squared is 2500.
8
>>>
Eggs !
50 squared is 2500.
8
27
>>>
Eggs !
50 squared is 2500.
8
False
>>>
Eggs !
50 squared is 2500.
8
>>>
Eggs !
50 squared is 2500.
8
27
False
>>> 


COMPLETION OF THE PIG LATIN WORK/LESSON.

When you think you've got it, SAVE and RUN your program and test it out!
"""
pyg = "ay"

if len(original) > 0 and original.isalpha():
     word = original.lower()
     first = word[0]

     if first == "a" or first == "e" or first == "i" or first == "o" or first == "u":
         print word + pyg
     else:

         rest = word[1:]
         print rest + first + pyg
        
    
else:
     print "empty"
    
Answer found below
Python 2.7.3 (default, Aug  1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: Apple
Apple
vowel
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: Bird
Bird
consonant
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: egg
egg

Welcome to the English to Pig Latin translator!
Please input an English word: egg
eggay
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: snake
nakesay
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: Mary
arymay
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: Agriculture
agricultureay
>>>
Welcome to the English to Pig Latin translator!
Please input an English word:

No comments:

Post a Comment