Lesson 4: Functions

Here are two syntax examples from the lesson, for your reference.


In [1]:
def hokeypokey(limb):
    print("You put your " + limb +" in,")
    print("You put your " + limb + " out,")
    print("You put your " + limb +" in,")
    print("and you shake it all about.")
    print("You do the hokey pokey and you turn yourself around,")
    print("and that's what it's all about!")
    print("")
    
print("Let's dance!!")
print("")
hokeypokey("left hand")


Let's dance!!

You put your left hand in,
You put your left hand out,
You put your left hand in,
and you shake it all about.
You do the hokey pokey and you turn yourself around,
and that's what it's all about!


In [2]:
def mult(a, b):
    return a * b

def sub(first, second):
    return first - second

def half(num):
    return num / 2


x = 50
y = 10
print(sub(half(mult(x, y)), x))


200.0

Mail merge. Write a function named sendEmail(address, name). It should take two parameters. One is the email address of the recipient. The other is the first name of the recipient. The function should print a message like this:

Sending email to [email address]. Hello [name]. I'm writing you today because you may be the hero our charity needs. Can you make a donation?

Then call the function a few times with different names and email addresses (they can be fake). Be sure the message prints out once for each person you call it for.


In [ ]:

Here's some example code for comparing multiple conditions at a time. You'll use it in the next problem. Here, just focus on understanding what it does. You can use the word "and" between two conditions. The whole thing will be true only if both of the sub-parts are true. You can use the word "or" between two conditions. The whole thing will be true if either one sub-part or the other is true (as well as if both are true).

AND means that every part must be true for the if statement to be true. OR means that at least one part must be true for the whole thing to be true.


In [7]:
weather="clear"
time="daytime"
if (weather=="clear" and time=="daytime"):
    print("It's a beautiful sunny day.")
elif (weather=="clear" and time=="night"):
    print("It's a good time to watch the stars.")
else:
    print("I didn't expect that weather.")


It's a beautiful sunny day.

In [8]:
num=12
if (num > 20 or num < 15):
    print("First condition")
if (num < 0):
    print("Negative number")


First condition

Here's a reminder of how modulus works to show the remainder after division.


In [10]:
a = 9
b = 4
print("a divided by b equals ", a / b)
print("a mod b equals ", a % b)


('a divided by b equals ', 2)
('a mod b equals ', 1)

FizzBuzz! Write a function that takes a number as input. If the number is evenly divisible by 2 with no remainder, print Fizz. If the number is evenly divisible by 10, print Buzz. If it's divisible by both, print both Fizz and Buzz. Otherwise, just print the number.

Note: You can calculate the remainder of A / B division by using the modulus % operator like this:

remainder = A % B

Then call the fizzbuzz function with the following numbers: 1, 3, 5, 9, 10, 12, 20, 25, 50, 60.


In [ ]:

Ani has found herself in another world. It's very similar but some names are different. Addition is called splond. Subtraction is called plink. There's a special word foon for taking a number, multiplying it by ten, and then adding five to it.

Write functions for splond, plink, and foon, that take 2 input parameters each. Then write several lines of code that call those functions and print the results so you can verify they are correct.


In [3]:
def splond(a, b):
    return a + b
def plink(a, b):
    return a - b
def foon(a, b):
    return a * 10 + 5

Check that your results are right. This statement should run properly.


In [ ]:
answer = foon(splond(3, 2), plink(5, 3))
if (answer == 55):
    print("You got it!")
else:
    print("Not quite - keep working on your functions.")

The next box is an experimental area for you to explore your own creative ideas!


In [ ]: