Lesson 304 05 jan14-1500-ay

Preview:

Citation preview

Unit 3: PythonLesson 4: Functions

January 5, 2014

2

Lesson 4: Functions

LoopsDesigning a Game

Working with Files

Lesson 8 Lesson 7 Lesson 6

Data Types

Lesson 5

Functions

Lesson 4

Boolean Logic

Lesson 3

Introduction to Programming

Hardware & Software

Lesson 1 Lesson 2

Putting It All Together

Lesson 12

Navigating the Web (?)

Lesson 11

Sorting and Searching

Advanced Algorithms

Lesson 9 Lesson 10

3

Recap from last time (I)

• Boolean Logic is a phrase used to describe the way computers think only in TRUE and FALSE

• AND, NOT, and OR can be used to combine statements together, but their meanings are a little different from their English meanings

• Remember that OR means one, or the other, or both!

Fish only

or or

Fish and ChipsChips only

4

Recap from last time (II)

• IF statements allow a computer to perform differently when in different situations

• Add ELSE to decide what will happen when the IF statement is FALSE

• Add ELIF when you have more than two cases to choose from

Manchester United wins! Liverpool wins! Penalty kicks

5

Functions are important to understand

• Computers often use functions to do all kinds of actions, whether it’s adding up numbers or even just showing us text on the screen

• To understand how we can program computers to do things for us, we first need to understand more about functions

6

A function is like a vending machine

• A function is a reusable section of code that performs a specific task

• Like a vending machine, a function is programmed to accept something from you (money) and give you something back in return (a fizzy drink)

7

Any task can be made into a function (I)

• When programming, you can write functions to do just about anything

• For example, you could write a function that will accept two numbers and add them together for you

Function

8

Any task can be made into a function (II)

• When programming, you can write functions to do just about anything

• For example, you could write a function that will accept two numbers and add them together for you

• Or you could write a function to accept a calendar date and figure out its day of the week

Function

Function

9

Functions don’t need to accept anything (I)

• Sometimes, functions will perform tasks that don’t require any input from you

• For example, you could have a function that adds all the numbers from 1 to 100 and returns the result

Function 5050

10

Functions don’t need to accept anything (II)

• Sometimes, functions will perform tasks that don’t require any input from you

• For example, you could have a function that adds all the numbers from 1 to 100 and returns the result

• You could even have a function that visits The Telegraph’s website and returns the top headline

Function 5050

Function“Drivers face 60mph speed limit on motorways due to EU pollution rules”

11

Writing functions in Python is easy!

• For a function that will say “Happy Birthday!”, you just need to write:

def birthday(): print “Happy Birthday!”

Don’t forget the colon!

12

Run this function by typing

• For a function that will say “Happy Birthday!”, you just need to write:

• Now that the function is written, every time we run birthday(), , the computer will wish us Happy Birthday!

def birthday(): print “Happy Birthday!”

Don’t forget the colon!

birthday()Happy Birthday!

Function Happy Birthday!

birthday()

13

Use words inside the parentheses to give the function an input

• We can rewrite our function to say any phrase by adding an input:

def parrot(phrase): print phrase

Any inputs go here

14

Include the input when running the function

• We can rewrite our function to say any phrase by adding an input:

• Now if we run birthday(), , the computer will wish us

def parrot(phrase): print phrase

Any inputs go here

Function Happy New Year!

parrot(“Happy New Year!”)

Happy New Year!

“Happy New Year!”

15

Exercise: Design a Dice Game (I)

• For this exercise, we’ll write a function of our own to create a simple dice game in Python

• First, open your internet browser, go to labs.codecademy.com, and click on the button for Python

16

Exercise: Design a Dice Game (II)

• Once the page finishes loading, you should see white space on the left. This is where we’ll be writing the code for our function

• The black space on the left is where we’ll run our code to start a game of dice!

We’ll start by writing code here

We’ll go here later to play dice

17

Exercise: Design a Dice Game (III)

• On the left side, let’s write the code shown below

Don’t forget the colon

Remember to tab these lines over

18

Exercise: Design a Dice Game (IV)

• If you look closely, you’ll see some familiar print statements and an IF statement. Let’s go through this line-by-line

19

Exercise: Design a Dice Game (V)

Pick a random number from 1 to 6

20

Exercise: Design a Dice Game (VI)

Pick a random number from 1 to 6Tell us what we rolled

21

Exercise: Design a Dice Game (VII)

Pick a random number from 1 to 6Tell us what we rolled

Pick another number from 1 to 6 and tell us what you rolled

22

Exercise: Design a Dice Game (VIII)

Pick a random number from 1 to 6Tell us what we rolled

Pick another number from 1 to 6 and tell us what you rolled

If our roll was larger, tell us we won!

23

Exercise: Design a Dice Game (IX)

Pick a random number from 1 to 6Tell us what we rolled

Pick another number from 1 to 6 and tell us what you rolled

If your roll was larger, tell us we lost

If our roll was larger, tell us we won

24

Exercise: Design a Dice Game (X)

Pick a random number from 1 to 6Tell us what we rolled

Pick another number from 1 to 6 and tell us what you rolled

If your roll was larger, tell us we lost

If our roll was larger, tell us we won

If the rolls were the same, tell us we tied

25

Exercise: Design a Dice Game (V)

• Now click and wait for a few seconds

• Once a second yellow arrow appears on the right side, type to play the game!

26

Summary (I)

• A function is a reusable section of code that performs a specific task

• Think of functions like vending machines – they can accept an input and return an output

• Sometimes functions don’t even need an input to return an output

Input money Output a fizzy drink

27

Summary (II)

• The syntax for writing a function in Python looks like this:

• You run this function by typing its name:

def parrot(phrase): print phrase

Any inputs go here

parrot(“Happy New Year!”)

Happy New Year!

28

What to do on your own

1. Go to URL to complete the Codecademy course online

2. Do the practice set on the material learned

3. Take the follow-up quiz to test your understanding