Académique Documents
Professionnel Documents
Culture Documents
Lesson 3
www.teachingcomputing.com
Series Overview
Information/Theory/Discuss
Task (Code provided)
Challenge (DIY!)
Suggested Project/HW
*Please note that each lesson is not bound to a specific time (so it can be taken at your own pace)
TYPE OF
LOOP
WHILE
LOOP
FOR LOOP
NESTED LOOP
DESCRIPTION OF LOOP
WHILE a given condition is TRUE, it repeats a statement or
group of statements. The stopping condition is found at the
START (tests it before looping)
A sequence of statements are executed multiple times. A for
loop is typically used when the amount iterations are known in
advance.
A loop of any kind can be used inside any other while, for or
dountil loop!
What is a loop?
Usually, statements are executed in sequence. This means the
second statement is run after the first statement and so on.
Sometimes you may need to execute a block of code or a
statement over and over again. This is where Loops are
particularly handy
A loop is a CONTROL
STRUCTURE that
allows for a statement
If condition is true
CONDITI
or group of
CONDITIONAL
ON
CODE
statements to be
Loops will need a
repeated
If condition is false
STOPPING CONDITION
(or starting condition)
import random
n = 10
guess_number = int(n * random.random()) + 1
guess = 0
print("****GUESSING THE RANDOMLY GENERATED
NUMBER*******")
while guess != guess_number:
guess = int(input("Enter Number: "))
if guess > 0:
if guess > guess_number:
print("Too high....try again")
elif guess < guess_number:
print("Too low...try a higher number")
else:
print("Ah...giving up so soon?")
break
else:
#this doesn't quite work. You'll notice that the tries, on each
go,
#stays at 1 (rather than counting up to 3). Can you fix it?
def login():
tries=0
while tries <1:
username="username1"
password="open123"
print('*********************************************')
print('Enter username')
answer1=input()
print('Enter password:')
answer2=input()
if answer1 == username and answer2 ==
password:
print("Access Granted")
Challenge 1: Solution
#A working solution - allowing a user to attempt login 3 times
def login():login()
tries=1
The While loop starts here. The stopping
while tries <4:
condition is: When tries is less than 4 (in other
username="username1"
words, stop the loop when the no. of tries
password="open123"
reaches 3)
print('*********************************************')
print('Enter username')
answer1=input()
print('Enter password:')
answer2=input()
if answer1 == username and answer2 == password:
print("Access Granted")
Incrementation: This tells the number of tries to
else:
keep going up by 1 for each loop.
print("Sorry, Access Denied")
count = 3
while (count < 5):
print ('The count is:', count)
count = count + 1
print ("Good bye!")
Count
Output (Print)
Challenge 2: Solution
Challenge 2 Solution
count = 1
while (count < 11):
print ('The count is:', count)
count = count + 1
print ("Good bye!")
It is a sequence that starts like this: Can you guess what comes
next?
1
3
1
/4VrcO6JaM
e
.b
u
t
u
o
/y
:/
s
p
htt
rM
Tlw7fNcO
/w
e
.b
u
t
u
o
/y
:/
s
http
-0
Notice the
pattern?
1+1 = 2
2+1 = 3
3+ 5 = 8
5+8 = 13
8+13 = ?
2
3
5
8
1
3 ?
Challenge 3: Solution
Change this from 2 to 1
Change this
from a to b!
https://youtu.be/d7e48cYq7uc
https://youtu.be/xd_laXe17Ak