Learning Intentions
To understand how While loops work Success Criteria Create while loops and use them in your python code |
Keywords
|
Starter - fill the screen -complete the task below
How did you do? can you still remember?
This is what it looks like.
Code 1
print ("I will always be fantastic in Computing lessons.\n" * 10)
We can also use a while loop to create the same effect
l
Code 2
>>>lines=0
>>>while lines < 50:
print ("I will always be fantastic in Computing lessons.")
lines = lines+1
give each code a try.
While code 2 is longer it uses a while loop. This is often more useful as you can do more complex tasks, try this code...
>>> number=1
>>> while number <101:
print(number)
number=number+1
So how do While loops work?
To start with you create a vairable and assign it a value. In the code above the while variable called "number" is less than 101.
All the code after that is indented and will be repeated by the computer (looped) until the number is more than 101
Copy these Operators into your work book
== equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
Important note - you use a double equals sign (==) to compare two values and a single equals sign (=) to assign a value to a variable .
Challenge 1 - write some code so that the computer counts up to 20 in twos
Challenge 2 - write some code so that the computer outputs the 5 times table like this
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
Hint you will need a counter vairiable which you could call (number). Then you should find out how to write one line and make your loop do it 10 times.
Challenge 3 - See if you can re-write the following code in three differnt ways so that each program still produces an output that counts to 100.
>>>number = 1
>>>while number < 101:
print (number)
number = number +1
You should not use the less than operator (<)
instead you should use these<= or > or !=
This is what it looks like.
Code 1
print ("I will always be fantastic in Computing lessons.\n" * 10)
We can also use a while loop to create the same effect
l
Code 2
>>>lines=0
>>>while lines < 50:
print ("I will always be fantastic in Computing lessons.")
lines = lines+1
give each code a try.
While code 2 is longer it uses a while loop. This is often more useful as you can do more complex tasks, try this code...
>>> number=1
>>> while number <101:
print(number)
number=number+1
So how do While loops work?
To start with you create a vairable and assign it a value. In the code above the while variable called "number" is less than 101.
All the code after that is indented and will be repeated by the computer (looped) until the number is more than 101
Copy these Operators into your work book
== equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
Important note - you use a double equals sign (==) to compare two values and a single equals sign (=) to assign a value to a variable .
Challenge 1 - write some code so that the computer counts up to 20 in twos
Challenge 2 - write some code so that the computer outputs the 5 times table like this
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
Hint you will need a counter vairiable which you could call (number). Then you should find out how to write one line and make your loop do it 10 times.
Challenge 3 - See if you can re-write the following code in three differnt ways so that each program still produces an output that counts to 100.
>>>number = 1
>>>while number < 101:
print (number)
number = number +1
You should not use the less than operator (<)
instead you should use these<= or > or !=