<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Introductions
Step1:  We can make this code into a function like so. Let's take a quick look at what we have here.
Step2:  Examine the function in detail
The top part is the header of the function. The header contains the keyword, 'def', the name of the function, a parameter, and a colon.
def is short for define. It's the Python keyword used to indicate the start of a function definition.
happy_birthday_carol is the name of the function.
This is a function with no parameters.
The body of our function contains one print line. We need to indent this line, and any other lines of code in a function's body, by 4 spaces.
Let's call the function
Notice that nothing was printed out after we defined our function.  To use our function we need to call it, like this
Step3:  Don't forget those parentheses.  
Without the parentheses, Python wouldn't know that we were trying to call our function, and will print out the value of the thing called happy_birthday_carol.
Step4:  Here, the Python interpreter is basically saying, "The value of happy_birthday_carol is a function."
Let's define and call another function
That function printed a nice greeting for Carol. But Rise's now feeling left out. Let's define a function for her too.
Step5:  Now, let's call the function
Step6:  Refactor time!
Ok, now, let's work on reusing our code so that we reduce code duplication.
Wouldn't it be cool if we could pass a name into our function so we don't need to write a different function for every name?
Python allows us to do just that with function parameters.  We can add a parameter by typing a variable name in between the parethesis after our function name.  We can then use the parameter variable in our function.
We'll create a new function called happy_birthday, and add a parameter, name.
Step7:  Here, we are concatenating 3 strings
Step8:  Calling it over and over in a loop
Let's get a little bolder and make a list of names that we can loop over and then pass each one into our happy_birthday function. What we get is the same results as the other earlier lines of code!
Step9:  Exercise
Step10:  Now, the function can print happy birthday greetings for any value of name, even for people with bizarre names like
Step11:  It's better to use format() just in case name isn't guaranteed to be a string.
Advanced Exercise
Step12:  Answer
Step13:  Now you have some advanced knowledge about how Python 3 handles Unicode better than Python 2!
Example
Step14:  This year, I turned 32.
Step15:  From the output of print_factors(), I now know that I'm
Step17:  Example
Step18:  How many cupcakes do we need for a party with 30 guests?
Step19:  What about if I'm celebrating my birthday at home with just my husband, Daniel?
Step20:  Ooh, 15 cupcakes sounds about right for our little party, yeah? ;)
Notice how we explicitly named the guests parameter in that example.
Printing the return value
Step21:  We call our function, cupcake_tally(), and pass in 10 as the value of guests. 
cupcake_tally(10) returns 33, which gets stored in the cupcakes variable.
Lastly, we print "We need to make 33 cupcakes." using Python string formatting.
Multiple function arguments
Let's talk about one last thing that functions can do and that is, they can be given more than one parameter. Let's demonstrate this by passing 2 parameters to our cupcake function.
We want to be able to specify the number of cupcakes each guest will eat.  Let's change our function to accept two parameters
Step22:  Note that we have 2 parameters now
Step23:  Look up when you're done, so that we know to move on to the next step.
Was this annoying?
Raise your hand if you felt like this exercise was repetitive...TODO
Modules
Modules are used to make a programmer's life easier. A module is used to group functions, variables, and other things together in an understandable bundle.
Python comes with lots of modules built-in.  For example there's a random module for generating random numbers.  Let's import it so we can use some of its functions
Step24:  The random module has a randint function which takes two parameters.  randint will return a random integer in between (or including) the two parameters.  For example we can generate a random number between 1 and 6.
Step25:  Let's use the random integer function to simulate rolling a die in a game
Step26:  choice is a function in the random module that returns a random item from a given list or string
Step27:  We could use random.choice to wish happy birthday to a random friend.
Let's make a function random_happy_birthday that takes a list of names and wishes a random person happy birthday
Step28:  Now let's try it out with a list of names
Step29:  Modules and functions can make things really simple to do. For example we could in a few lines of code get and display a YouTube video
<ASSISTANT_TASK:>
Python Code:
print("Happy Birthday, dear Carol!")
Explanation: Introductions: Hi, I'm .... I'll be co-facilitating 'Programming with Python' with [our mentors names] and [our volunteers names]. If you would like to follow along and type in some of the snippets of code that we demo, our volunteers can assist you in setting up your computer. 
Let's begin by giving a quick recap on what we did in the earlier workshops today. In the first workshop we learned about numbers, variables, strings, booleans, and making choices with the use of if-statements. In the second workshop we added lists and looping to our skills. Along the way we used a few functions, such as the built-in functions, print() and len().
In this workshop, we're going to go a little further in depth into functions and Python modules.
Functions
We have already seen a few functions (recall len() for calculating list and string lengths and sum() for summing lists).
Python also lets us make our own functions.
Example: Greeting Program
Say we have a simple program that prints a happy birthday greeting for Carol.
It's not Carol's birthday, but let's run this program anyway, just to confuse her in good fun ;)
End of explanation
def happy_birthday_carol():
    print("Happy Birthday, dear Carol!")
Explanation: We can make this code into a function like so. Let's take a quick look at what we have here.
End of explanation
happy_birthday_carol()
Explanation: Examine the function in detail
The top part is the header of the function. The header contains the keyword, 'def', the name of the function, a parameter, and a colon.
def is short for define. It's the Python keyword used to indicate the start of a function definition.
happy_birthday_carol is the name of the function.
This is a function with no parameters.
The body of our function contains one print line. We need to indent this line, and any other lines of code in a function's body, by 4 spaces.
Let's call the function
Notice that nothing was printed out after we defined our function.  To use our function we need to call it, like this:
End of explanation
happy_birthday_carol
Explanation: Don't forget those parentheses.  
Without the parentheses, Python wouldn't know that we were trying to call our function, and will print out the value of the thing called happy_birthday_carol.
End of explanation
def happy_birthday_rise():
    print("Happy Birthday, dear Rise!")
Explanation: Here, the Python interpreter is basically saying, "The value of happy_birthday_carol is a function."
Let's define and call another function
That function printed a nice greeting for Carol. But Rise's now feeling left out. Let's define a function for her too.
End of explanation
happy_birthday_rise()
Explanation: Now, let's call the function:
End of explanation
def happy_birthday(name):
    print("Happy Birthday, dear " + name + "!")
Explanation: Refactor time!
Ok, now, let's work on reusing our code so that we reduce code duplication.
Wouldn't it be cool if we could pass a name into our function so we don't need to write a different function for every name?
Python allows us to do just that with function parameters.  We can add a parameter by typing a variable name in between the parethesis after our function name.  We can then use the parameter variable in our function.
We'll create a new function called happy_birthday, and add a parameter, name.
End of explanation
happy_birthday("Trey")
Explanation: Here, we are concatenating 3 strings:
* "Happy Birthday, dear "
* whatever the value of name is (Note: for the purposes of this example, let's assume that we're always getting a string)
* "!"
To execute or run our new function, we simply call it by its name, happy_birthday, and pass a value in between the parenthesis, such as the string Trey.
End of explanation
names = ["Carol", "Rise", "Trey", "Alain"]
for name in names:
    happy_birthday(name)
Explanation: Calling it over and over in a loop
Let's get a little bolder and make a list of names that we can loop over and then pass each one into our happy_birthday function. What we get is the same results as the other earlier lines of code!
End of explanation
def happy_birthday(name):
    print("Happy Birthday, dear {0}!".format(name))
Explanation: Exercise: Try to break happy_birthday()
1) Try calling happy_birthday() with other values, such as:
* A really, really, really long name
* Something ridiculous that makes no sense grammatically
* Numbers
* Other Python types, if you know about any other ones
This is how malicious "black hat" hackers think when they try to break into desktop and web applications.
2) Try adding a bad list item to names. Experiment with the position of the bad item.
Example: Better String Formatting
Rather than using string concatenation, let's use a string formatting operator that doesn't care what type of value it receives.
End of explanation
happy_birthday(100)
happy_birthday(3.1415927)
happy_birthday("Ƭ̵̬̊")
Explanation: Now, the function can print happy birthday greetings for any value of name, even for people with bizarre names like:
* 100
* 3.1415927
* Ƭ̵̬̊ (The Artist Formerly Known As Prince)
End of explanation
def happy_birthday(name):
    print("Happy Birthday, dear " + str(name) + "!")
Explanation: It's better to use format() just in case name isn't guaranteed to be a string.
Advanced Exercise: Why not use str() like this?
End of explanation
str(u'\xff')
Explanation: Answer: You can, but keep in mind that this only works on Python 3:
End of explanation
def print_factors(age):
    for i in range(1, age + 1):
        if age % i == 0:
            print(i)
Explanation: Now you have some advanced knowledge about how Python 3 handles Unicode better than Python 2!
Example: Getting Fancy With Math
What about a function that does some math for us?
Every year on my birthday, I like to figure out what numbers are divisible by my new age. That is, the factors of my age:
End of explanation
print_factors(32)
Explanation: This year, I turned 32.
End of explanation
print_factors(33)
Explanation: From the output of print_factors(), I now know that I'm:
* Twice as old as a 16-year-old
* Four times as old as an 8-year-old
* Eight times as old as a 4-year-old
* Sixteen times as old as a 2-year-old
How is this useful? Well, the next time a 4-year-old bullies me, I'm going to say, "Watch it, I'm exactly eight times as old as you."
Next year, I'll be able to brag to 3-year-olds: "Ha ha ha, I'm exactly eleven times as old as you."
End of explanation
def cupcake_tally(guests):
     Given number of party guests, returns how many cupcakes are needed. 
    return 2 * guests + 13
Explanation: Example: Cupcake Tally
So far we've explored functions that:
* Take strings or other values as input
* Print strings
Let's create a new function, cupcake_tally(). This function will be different from the others that I covered. Rather than just printing stuff, it'll return a value when called.
A good birthday party should allocate:
* 2 cupcakes per guest
* 13 cupcakes for the birthday person, because if it's your birthday, you should be allowed to eat a baker's dozen of cupcakes.
End of explanation
cupcake_tally(30)
Explanation: How many cupcakes do we need for a party with 30 guests?
End of explanation
cupcake_tally(guests=1)
Explanation: What about if I'm celebrating my birthday at home with just my husband, Daniel?
End of explanation
cupcakes = cupcake_tally(10)
print("We need to make {0} cupcakes.".format(cupcakes))
Explanation: Ooh, 15 cupcakes sounds about right for our little party, yeah? ;)
Notice how we explicitly named the guests parameter in that example.
Printing the return value
End of explanation
def cupcake_tally(cupcakes, guests):   
    return cupcakes * guests + 13
cupcake_tally(4, 15)
Explanation: We call our function, cupcake_tally(), and pass in 10 as the value of guests. 
cupcake_tally(10) returns 33, which gets stored in the cupcakes variable.
Lastly, we print "We need to make 33 cupcakes." using Python string formatting.
Multiple function arguments
Let's talk about one last thing that functions can do and that is, they can be given more than one parameter. Let's demonstrate this by passing 2 parameters to our cupcake function.
We want to be able to specify the number of cupcakes each guest will eat.  Let's change our function to accept two parameters:
End of explanation
print("On the day of Sept 20, 2014, were you at Intro to Python?")
answer = input("Answer truthfully, yes or no --> ")
if answer == "no":
    answer = input("Are you sure that you weren't? Tell the truth, now --> ")
print("Were you thinking of skipping the workshop to go to Sea World?")
answer = input("Answer truthfully, yes or no --> ")
if answer == "no":
    answer = input("Are you sure that you weren't? Tell the truth, now --> ")
Explanation: Note that we have 2 parameters now: cupcakes and guests
Our function is called and passes in two values of 4 and 15. It returns 73.
Summary: What are functions used for?
Program decomposition, or factoring
To break down a problem into parts. It makes the code more readable and is easier to understand.
Code reuse
Functions can be used instead of repeating the same lines of code at different times throughout a program. This reduces duplication of code.
Abstraction or simplification
Using functions allows us to hide all of the details involved and put them into one place and simply call the function to execute the lines of code.
Exercise: Why are functions useful?
First, create a new Python script and type in the following.
End of explanation
import random
Explanation: Look up when you're done, so that we know to move on to the next step.
Was this annoying?
Raise your hand if you felt like this exercise was repetitive...TODO
Modules
Modules are used to make a programmer's life easier. A module is used to group functions, variables, and other things together in an understandable bundle.
Python comes with lots of modules built-in.  For example there's a random module for generating random numbers.  Let's import it so we can use some of its functions:
End of explanation
random.randint(1, 6)
Explanation: The random module has a randint function which takes two parameters.  randint will return a random integer in between (or including) the two parameters.  For example we can generate a random number between 1 and 6.
End of explanation
vegas_die_1 = random.randint(1, 6)
vegas_die_2 = random.randint(1, 6)
print("First die: " + str(vegas_die_1))
print("Second die: " + str(vegas_die_2))
print("You rolled a " + str(vegas_die_1 + vegas_die_2))
Explanation: Let's use the random integer function to simulate rolling a die in a game
End of explanation
random.choice('abcdefghijklmnopqrstuvwxyz')
Explanation: choice is a function in the random module that returns a random item from a given list or string
End of explanation
def random_happy_birthday(names):
    name = random.choice(names)
    happy_birthday(name)
Explanation: We could use random.choice to wish happy birthday to a random friend.
Let's make a function random_happy_birthday that takes a list of names and wishes a random person happy birthday
End of explanation
random_happy_birthday(["Alain", "Carol", "Rise", "Trey"])
Explanation: Now let's try it out with a list of names:
End of explanation
from IPython.display import YouTubeVideo
# a tutorial about Python at PyCon 2014 in Montreal, Canada by Jessica McKellar
YouTubeVideo('MirG-vJOg04')
Explanation: Modules and functions can make things really simple to do. For example we could in a few lines of code get and display a YouTube video
End of explanation
<END_TASK>