year 7 lesson 3 variables and while loops
Post on 23-Jun-2015
1.162 Views
Preview:
DESCRIPTION
TRANSCRIPT
VARIA
BLES
& WHILE
LOOPS
L ES
SO
N 3
LESSON OBJECTIVES
Learn to use variables and ‘while’ loops to get keyboard input from users.
LESSON OUTCOMES
I can use variables in Python …
I can use while loops in Python …
VARIABLES[ slide 3 ]
A variable is a space in the computer’s memory where we can store data such as a number or text. The value can be changed if required.
The box represents the space reserved in the computer’s memory
To create a variable and give it the value 42 we write:
>>> my_number=42my_numbervariable
name
42The variable’s value
Outcomes: I can use variables in Python… I can use while loops in Python …
>>> my_number=0while my_number < 10:
print (I love coding in Python)
my_number = my_number+1
The variable is called number
The = operator gives the variable a value 0.
This line of code says ‘while the variable called number is less than 10 do the following.
All of the code that is indented after the colon is to be repeated by the computer – it loops through these two lines of code until number is no longer less than 10.
WHILE LOOPS
Outcomes: I can use variables in Python… I can use while loops in Python …
TASK 1 – USING WHILE LOOPS[ slide 4 ]
Open the following file: While loop.
When the file is open enter your own print output in the print function brackets.
>>> my_number=0
>>> while my_number < 10:
print("I love coding in Python!")
my_number = my_number+1
Outcomes: I can use variables in Python… I can use while loops in Python …
When you have completed the above code attempt Task 1 on worksheet 3.
MINI PLENARY
Look at the following code:
x=0
while x<5:
print('looping')
When do you think the code will stop looping.
Outcomes: I can use variables in Python… I can use while loops in Python …
MAIN TASK 2
Have a go at completing Tasks 2 and 3
on the worksheet.
Outcomes: I can use variables in Python… I can use while loops in Python …
WORKSHEET ANSWERS
Outcomes: I can use variables in Python… I can use while loops in Python …
PLENARY
Can you explain what the program lines are doing?
Outcomes: I can use variables in Python… I can use while loops in Python …
ANSWER
Outcomes: I can use variables in Python… I can use while loops in Python …
top related