Professional Documents
Culture Documents
ECT Lesson Computer Science
ECT Lesson Computer Science
Core subject(s)
Computer Science
Subject area(s)
Suggested age
13 to 18 years old
Prerequisites
None
Lesson Overview
Materials and Equipment
Preparation Tasks
The Lesson
Learning Objectives and Standards
Additional Information and Resources
Time
Preparation: 5 to 15 minutes
Instruction: 80 to 115 minutes
Standards
Lesson Overview
In computational thinking, the patterns we recognize in the world can be abstracted into functions. If we have a function
we can give it the ability to loop quickly, use logic to make decisions, and more. By designing algorithms into functions
we are able to better understand and use the laws of our world. Students will be able to identify, evaluate, follow and
create functions including functions that loop, functions that include decisions and functions that include both. The
activities increase in difficulty and students should continue as far as they are able to.
Preparation Tasks
Confirm that all students computers are turned on, logged-in, and connected to
the Internet
1 to 10 minutes
3 to 10 minutes
The Lesson
Warm-up Activity: What are functions and algorithms?
15 to 20 minutes
15 to 20 minutes
20 to 30 minutes
20 to 30 minutes
10 to 15 minutes
1 of 12
2 of 12
Activity:
Write the following definitions of function and algorithm in a place where your students may refer back to it:
Functions take input data like numbers, words, pictures, etc. and return an output.
Algorithms are the instructions, steps, or recipe to get from the inputs to the output.
4-S Brainstorming: All students respond to the following prompt provided by the teacher.
Write an algorithm that provides an example of a function from your everyday life.
Four students will be selected by the teacher to play a specific role in helping the rest of the class generate the
maximum number of responses.
Accelerator - Student encourages classmates to generate more ideas (Lets get more ideas, only two minutes left)
Acceptor - Student helps classmates commit to an idea (All ideas are OK, write that one down)
Exaggerator - Student encourages classmates to generate different kinds of ideas (We need some silly ideas)
Connector/recorder - Student makes the connection between different ideas and writes them down (Which ideas are
connected to that?)
The teacher will facilitate the activity
After 10 minutes the teacher will collect the responses. Key themes related to the activity will be written on the board.
Activity:
Have students do the following and and answer questions:
Run the examples below in your Python Interpreter. >>> is the interpreter prompt, so you type what comes
after >>>. If your interpreter has color, the program prints the blue text. If the program types a question, you
type an answer and press Enter.
3 of 12
# Lines that start with a # are comments and are ignored by Python.
>>> 2 + 5
7
>>> r = input("What is the radius of your circle? ")
What is the radius of your circle? 10
>>> a = 3.14*(r**2) #pi times r squared
>>> print "Area = " + str(a)
314.0
>>> name = input("What is your name? ")
What is your name? "Arthur"
>>> print "Hello " + name
Hello Arthur
There are 5 functions in the examples above can you find them all?
Why did we need to convert a into a string of characters in print "Area = " + str(a)?
Rewrite the third example to calculate circumference instead.
Create a function that converts units (e.g. meters feet, Celsius Fahrenheit).
Create a function that asks for your age and prints out how many seconds you have been alive.
Q1: There are 5 functions in the above examples, can you find them all?
Q2: Why did we need to convert a into a string of characters in print "Area = " + str(a)?
Q3: Rewrite the third example to calculate circumference instead.
Student Assessment:
A1: Addition "+" (of numbers and strings), input(), multiplication "*", print, and str().
A2: The number a and the words Area are not the same type and needed to be converted to show on the same line.
A3: c = 3.14*2*r
print "Area = " + str(c).
Assessment:
A1: Possible answers:
4 of 12
Activity:
Take students through the following with student-teacher interaction:
The sample assessment answers illustrate three types of repeating function:
Loops forever (every day or every year)
Loops a certain number of times (sing the chorus 3 times)
Loops while something is true (while there is gasoline and the car is on)
When writing a loop, the initial line ends with a colon ":". The next lines requires four spaces of indentation to
show which code is inside the loop and which code is not. Other languages use brackets { } to do this. In IDLE,
Python will automatically index the code as long as you include the colon.
Example of a loop that continues forever (infinite loop):
>>> count = 0
>>> while 1 == 1: #The loop will repeat forever until you stop Python.
...
count += 1 #Adds 1 to count each time it loops
...
print count
1
2
3
... #and so on forever - to stop hold control key and touch the c key
Q1: Can you imagine a few other ways to create an infinite loop with the while loop?
Q2: Why does count = 0 use only one equals sign but while 1 == 1: uses two?
Q3: What is another way count += 1 could be written?
Assessment:
A1: while 3 == 3, while 1, while True, etc. As long as the test is always true.
A2: The first is storing zero in the variable count; the second is a test to see if one is equal to one.
A3: count = count + 1
Activity:
Share the following with your students:
Example of a loop that only repeats a few times:
>>> count = 3
>>> while count > 0: #Checks each time to see if count is greater than
zero.
...
print count
...
count -= 1 #Subtracts 1 from count each time it loops
3
5 of 12
2
1
>>> for count in range(3, 0, -1): #Another way to say the same as above
in Python
print count
Q4: In the second version of the loop above, why is the range from 3 to 0 when the loop prints out the numbers
3, 2, 1?
Q5: For the second loop, change it to for count in range(1,6,2): what does the third number do? What
would it do if it was changed to 3?
Assessment:
A4: The function is range(start,end) including start but excluding end.
A5: The third number is the step or how many numbers to skip each time. If it was changed to 3 then the output would
be 1 and 4.
Activity:
Share the following with your students:
The functions so far use calculations. Functions can also use logic to make decisions and take different
actions based on these decisions. When you combine repeating algorithms and logic, there is enough
intelligence to make decisions. That is the power of computational thinking.
Humans use their own logic in the functions they use everyday:
Which shirt matches my socks?
How much sugar and cream should I add to my coffee to make it perfect?
What is the fastest way to school while staying dry in the rain?
Example of logic that determines which name is first in alphabetical order:
>>> name1 = input("What is the first name? ")
What is the first name? "Alice"
>>> name2 = input("What is the second name? ")
What is the second name? "Bob"
>>> if name1 < name2:
...
print name1
else:
...
print name2
Alice
6 of 12
Example of a Guess The Number Game (you may wish to save the code so you can play it again):
>>> from random import *
>>> correct_number = randint(1,10) #Computer picks a number from 1 to
10
>>> guess = 0 #This is set to zero so the loop begins.
>>> while guess != correct_number:
...
guess = input("What number am I thinking of (between 1-10)? ")
...
if guess > correct_number:
...
print "Too high"
...
if guess < correct_number:
...
print "Too low"
...
if guess == correct_number:
...
print "You got it!"
Example output:
What number am I thinking of (between 1-10)? 5
Too High
What number am I thinking of (between 1-10)? 3
Too Low
What number am I thinking of (between 1-10)? 4
You Got it!
Q1: Why does the first loop put the inputs in alphabetical order with the < sign? Isnt < only for numbers?
Q2: What would happen if you ran the Guess The Number game again but skipped the second and third lines?
Assessment:
A1: Operator overloading is when the < sign can compare 1 < 2 and 'a' < 'b'. Another example of operator
overloading is 2 + 3 and 'Py' + 'thon'.
A2: The game would not pick a new random number, guess would not be reset to 0, therefore the condition to run the
loop would not be met. Python would also complain that 'guess' is not defined.
Activity:
Share the following with your students:
Here is an example of a loop that acts like a Thermostat in your home:
>>> set_temp = 70
>>> current_temp = 70
>>> while 1 == 1:
...
set_temp = input("What do you want the temperature to be? ")
...
if current_temp > set_temp:
...
print "Turning on the air conditioning!"
...
while current_temp > set_temp:
...
current_temp -= 1
...
print "Current Temp: " + str(current_temp)
...
if current_temp < set_temp:
...
print "Turning on the heater!"
...
while current_temp < set_temp:
...
current_temp += 1
...
print "Current Temp: " + str(current_temp)
Q3: Instead of having the infinite loop in the beginning of the code, how could you modify the code to only run
7 of 12
once?
Q4: Imagine you had a more powerful air conditioning or heater that added two degrees each time through the
loop instead one. What might be a possible issue?
Assessment:
A3: There are many possible options but the easiest one would be to modify the first loop to say
while current_temp != set_temp.
A4: If nothing else was changed in the code, you might overshoot the set_temp. This already happens in your homes
but negative feedback loops and sensors work to prevent this.
Standards
Location
Core Standards
CCSS MATH.PRACTICE.MP4: Model with mathematics.
Warm-up
Activity
8 of 12
Core Standards
CCSS MATH.CONTENT.8.F.A.1: Understand that a
function is a rule that assigns to each input exactly one
output. The graph of a function is the set of ordered pairs
consisting of an input and the corresponding output.
Activity 1
Core Standards
CCSS MATH.CONTENT.8.F.B.4: Construct a function to
model a linear relationship between two quantities.
Determine the rate of change and initial value of the
function from a description of a relationship or from two
(x, y) values, including reading these from a table or from
a graph. Interpret the rate of change and initial value of a
linear function in terms of the situation it models, and in
terms of its graph or a table of values.
Activity 1
Core Standards
CCSS MATH.PRACTICE.MP2: Reason abstractly and
quantitatively.
Activity 2
9 of 12
Core Standards
CCSS MATH.PRACTICE.MP2: Reason abstractly and
quantitatively.
Activity 2
Core Standards
CCSS MATH.PRACTICE.MP2: Reason abstractly and
quantitatively.
Activity 3
Core Standards
CCSS MATH.CONTENT.8.F.B.4: Construct a function to
model a linear relationship between two quantities.
Determine the rate of change and initial value of the
function from a description of a relationship or from two
(x, y) values, including reading these from a table or from
a graph. Interpret the rate of change and initial value of a
linear function in terms of the situation it models, and in
terms of its graph or a table of values.
Wrap-up
Definition
Location
Function
http://en.wikipedia.org/wiki/Function
Warm-up
Activity
10 of 12
Algorithm
http://en.wikipedia.org/wiki/Algorithm
Warm-up
Activity
Boolean logic
http://en.wikipedia.org/wiki/Boolean_alg
ebra
Activity 3
Python
interpreter/shell
https://docs.python.org/2.7/tutorial/inter
preter.html
Activity 1
Negative
feedback
https://en.wikipedia.org/wiki/Negative_f
eedback
Activity 3
Definition
Location
Algorithm
Design
Warm-up
Activity
Description
Location
Meaningful and
Relevant
Content
Warm-up
Activity
Student-Teacher
interaction
Interactions between students and teachers that encourages students to feel less
intimidated and more included during novel tasks
Activity 2
Interdisciplinary
Connections to
CS
The integration of knowledge and skills from other disciplines (e.g. Biology,
Finance) with computer science
Activity 3
Administrative Details
Contact info
For more info about Exploring Computational Thinking (ECT), visit the ECT website
(g.co/exploringCT)
Credits
Developed by the Exploring Computational Thinking team at Google and reviewed by K-12
educators from around the world.
Last updated on
07/02/2015
11 of 12
Copyright info
Except as otherwise noted, the content of this document is licensed under the Creative Commons
Attribution 4.0 International License, and code samples are licensed under the Apache 2.0 License.
12 of 12