Loading...
Hello.
My name is Mr. Hogan.
I am excited to be learning with you today.
We are going to have such a great time learning all about programming iteration.
I will be supporting you with our learning during these lessons.
I am so pleased that you've decided to learn about while loops.
We are going to do brilliantly.
The outcome of today's lesson is that I can use iteration controlled by a condition to repeat sequences of code.
So hopefully by the end of this lesson, you will be able to do this.
We have two keywords this lesson, iteration, which is the process of repeating a sequence of instructions within a programme loop, and we also have a condition.
This is an expression that evaluates to True or False.
Don't worry if you don't really understand these at the moment.
These will be explained during the lesson, and you can always come back to this slide to remind yourself about the definitions.
There are two learning cycles in this lesson.
The first one is explain how iteration is used in programming.
The second one is modify code to iterate while a condition is met.
I'm really looking forward to doing these learning cycles with you.
They're really, really good.
I'm really excited about it.
Let's look at this first one, explain how iteration is used in programming.
Alex has created this programme for a user to enter a passcode to unlock a phone.
So if you think about your phone, what actually is the programme behind it that allows you to unlock it? So we can see that in line 1, there's a print statement that outputs "Enter the password to unlock." So that's the sort of message you will see on your phone.
And line 2, is the passcode equals to the input? So if the value held in the passcode variable is the same as what the user enters, then we've got the third line with a print statement on it, which outputs "The passcode you have entered is {passcode}".
Alex can make the programme more efficient, though.
We can combine the prompt message directly with the input() function.
So now we only have two lines of code.
We've removed the first print statement that simply repeats the input request, which is not needed.
So now line 1 is passcode = input ("Enter the passcode to unlock").
So we've combined those lines of code.
This approach is specific to Python and saves a line of code.
The input() function in Python allows you to display a prompt message with the parentheses ().
So now you can see the comparison between the three lines of code which Alex first used compared to the two lines of code.
So Alex is saying, "Great, I don't have to type the first line of code.
What if I want the programme to repeat until a specific passcode, like 1978, is entered?" Good question, Alex.
Let's see if we can find out.
So iteration is the process of repeating a sequence of instructions a number of times.
In a programme, a loop statement can be used to repeat a sequence of instructions.
Condition-controlled iteration is when a set of instructions is repeated based on whether a condition evaluates as True or False.
Let's have a quick check for understanding.
So it's a true or false one.
Condition-controlled iteration is when a set of instructions is repeated based on whether a condition evaluates as True or False.
Have a think about your answer.
Okay, what is the answer? It is true.
Why is it true? Well, a condition-controlled iteration, such as with a while loop, continues as long as a specific condition remains True.
Condition-controlled iteration is implemented using while loops in Python.
A while loop will repeat actions while a condition is True.
So we can see this represented on the right-hand side of the slide.
We've got while condition, whatever that condition is, so while that's True, the block of code will run underneath it.
Or in a flow chart, we've got a decision block there, the diamond, where if it is True, other blocks of code, those two blocks of code or could be two lines of code, will run, and then we'll repeat, we'll loop back.
Can you predict what the outcome of running this programme will be? Let's have a look at this code in a bit more depth.
These statements are to be repeated.
This condition on line 2 is checked at the beginning of each loop.
So while the passcode is not equals to 1978, repeat the code below.
Let's have a quick check.
What determines how long a while loop continues to repeat its actions in Python? Is it A, the number of lines in the code, B, a condition, or C, a random number? Take your time to think about it, and you can pause the video at any time or even rewind it to remind yourself of the answer.
Okay, shall we take a look at the answer? It is B, a condition.
This programme will keep asking the user for the passcode until the passcode "1978" is entered, at which point it will display "Hello welcome to your phone".
You can try the code at oak.
link/ask-code, with a hyphen in between ask and code, to test the programme and see how it works.
You can take some time now to do that.
Now it's time for a practise.
1, what does iteration mean in programming? 2, decide how a while loop is used in Python.
So you can take some time to think about your answer, and you can always rewind the video.
Let's have a look at the answers, or some suggested answers.
So 1, what does iteration mean in programming? So iteration is repeating a set of instructions lots of times.
It's like doing the same thing over and over again.
This allows programmers to use a loop statement to automate repetitive code.
Let's have a look at answer 2.
Question is, describe how a while loop is used in Python.
A while loop is a loop used to control iteration in Python.
It's called condition-controlled because it repeats a block of code as long as a specific condition is met.
So hopefully you've got your answers very similar to this, but don't worry if you didn't.
You can always pause this video, rewind it to remind yourself about the content of the lesson and the knowledge you should have, and then you could perhaps try the questions again.
Well done for getting this far as well.
We're gonna move on.
We're going to move on now to the second learning cycle in this lesson.
I'm really looking forward to it.
It is to modify code to iterate while a condition is met.
So you're actually going to modify some Python code.
Logical operators can be used in selection statements to control programme flow.
The if statement in this code checks if the value of the card is greater than 6.
So on line 2, if the value is greater than 6, so the variable value, so on line 1, we get the user to input a value as an integer, and that's stored in the variable value.
And if that is above 6, we can say print("My card is a match") else: print("My card is not a match").
So we can use these if statements to check the value of the card is greater than 6.
Sofia has created a guess the number game.
So, we may have seen some code like this before, but if we haven't, don't worry.
Let's look at it now.
So, on line 1, we've got the variable number, and the number, the integer 4 is stored in that number.
And then on line 2, we've got the integer input here where the user inputs a number between 1 and 10.
And then we've got if guess is not equals to the number variable, or the value held in the number variable, print("Incorrect") else: print("Correct").
But what Sofia's correctly asking, a really good question, "What if the user does not get it correct at the first attempt?" Sofia could repeat the code.
The first six lines are the same as the programme we've seen before, and all she's done is just repeated the code, so we repeat guessing the number.
It's time for a quick check.
How many times would the user get to guess the number using this programme? Is it A, one, B, two, or, C3? Remember, you can pause the video at any time or go back to previous slides to help you.
Shall we take a look at the answer? It is B, two times, so the user would get two guesses.
Sofia's asking another really good question, "What if the user does not get it correct at the second attempt?" The code is using selection which is not the most efficient for this purpose.
A while loop is more efficient because it allows guessing until the correct number is entered.
So this is the original code.
So we've just got the one if-else selection there.
To make the code more efficient, you can replace the repeated if blocks with a while loop.
So here we can see on line 3, we've got the while loop.
The while loop starts and keeps running as long as the guess is incorrect.
So on line 3, where we've got while guess != number: This means the user will be asked to guess again until they enter the correct number.
When the condition becomes false, the loop stops.
The programme then executes the next line of code, which prints "Correct!" on line 6.
Let's have a quick check.
What condition keeps the while loop running in a number guessing programme? Is it A, when the guess is correct, B, when the guess is incorrect, or C, when the guess is greater than the number? So, take your time to think about the answer.
Remember, you can pause or rewind the video at any time.
Let's have a look at the answer.
It is B, when the guess is correct.
Hopefully you're having a really good time.
I am.
We're gonna move on to the practise now for this learning cycle.
So number 1, amend the programme to keep asking for a password until the correct one is entered.
Okay, so we've got some if statements here.
Can you actually amend the programme to keep asking for a password until the correct one is entered? Remember, take your time to think about the answer.
You can pause or rewind the video at any time.
Here's the answer.
So yeah, we've gotta put a while loop here in on line 3.
So while guess != password: print("Incorrect password.
Try again.") And then again, ask on line 5 for an input to enter the password, and go back to line 3.
So that is the while loop repeating.
When that becomes false, that line 3 becomes false, we then go to line 6 and print "Access granted!" Well done if you got this correct.
2, modify your programme so that the user has a maximum of three attempts to enter the correct password.
If they fail, display "Too many incorrect attempts.
Access denied!" and end the programme.
You'll need a counter variable to track how many attempts have been made, and you may use the and logical operator.
So there's a couple of hints there.
Take your time.
Remember, you can pause the video at any time or rewind it.
So let's have a look at a possible answer.
Hopefully yours is similar to this, but there may be slightly different ways to do it.
So, on line 1, we defined the correct password as "oak123".
This is the value the user needs to guess.
On line 2, we set the maximum number of attempts to 3.
This means the user only gets three chances to guess the password.
On line 4, the programme asks the user to enter the password.
Whatever they type is stored in a variable called guess.
On line 5, this starts a while loop.
The loop continues only if the guess is not equals to the password, and the password, the user, sorry, still has more than one attempt left.
In other words, we stop looping when the user either gets it right or runs out of attempts.
On line 6, this reduces the number of attempts by one.
This happens each time the user guesses incorrectly.
Line 7 gives the user feedback showing how many attempts are left.
Line 8 gives the user another chance to enter the password.
Lines 10 to 13, so this is after the loop ends, line 10 checks whether the final guess is correct.
If it is, the programme prints "Access granted!" on line 11.
If not, line 13 shows "Too many incorrect attempts.
Access denied!" Meaning the user failed all their chances.
So take your time, have a look at the code, and well done if you got it all correct.
If not, you may want to update your own code.
Awesome, we have reached the end of the lesson.
So, in summary, iteration in Python is implemented through while loops, which allow you to repeat actions as long as specified conditions remain True.
By modifying code to include a while loop, we can repeat instructions until a specific condition is True.
Well, I hope you enjoyed the lesson.
I really did.
Remember, you can rewind the video to look at any part at any point.
Well done.