lesson 304 05 jan14-1500-ay

28
Unit 3: Python Lesson 4: Functions January 5, 2014

Upload: codecademy-ren

Post on 13-May-2015

6.632 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Lesson 304 05 jan14-1500-ay

Unit 3: PythonLesson 4: Functions

January 5, 2014

Page 2: Lesson 304 05 jan14-1500-ay

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

Page 3: Lesson 304 05 jan14-1500-ay

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

Page 4: Lesson 304 05 jan14-1500-ay

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

Page 5: Lesson 304 05 jan14-1500-ay

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

Page 6: Lesson 304 05 jan14-1500-ay

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)

Page 7: Lesson 304 05 jan14-1500-ay

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

Page 8: Lesson 304 05 jan14-1500-ay

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

Page 9: Lesson 304 05 jan14-1500-ay

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

Page 10: Lesson 304 05 jan14-1500-ay

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”

Page 11: Lesson 304 05 jan14-1500-ay

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!

Page 12: Lesson 304 05 jan14-1500-ay

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()

Page 13: Lesson 304 05 jan14-1500-ay

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

Page 14: Lesson 304 05 jan14-1500-ay

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!”

Page 15: Lesson 304 05 jan14-1500-ay

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

Page 16: Lesson 304 05 jan14-1500-ay

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

Page 17: Lesson 304 05 jan14-1500-ay

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

Page 18: Lesson 304 05 jan14-1500-ay

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

Page 19: Lesson 304 05 jan14-1500-ay

19

Exercise: Design a Dice Game (V)

Pick a random number from 1 to 6

Page 20: Lesson 304 05 jan14-1500-ay

20

Exercise: Design a Dice Game (VI)

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

Page 21: Lesson 304 05 jan14-1500-ay

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

Page 22: Lesson 304 05 jan14-1500-ay

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!

Page 23: Lesson 304 05 jan14-1500-ay

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

Page 24: Lesson 304 05 jan14-1500-ay

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

Page 25: Lesson 304 05 jan14-1500-ay

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!

Page 26: Lesson 304 05 jan14-1500-ay

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

Page 27: Lesson 304 05 jan14-1500-ay

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!

Page 28: Lesson 304 05 jan14-1500-ay

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