video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher and welcome to Lesson 6 of this Python unit.

Well done for getting this far, this is your last lesson.

Hopefully you've learned a lot and you've made a lot of progress and it'll all begin to come together in this final lesson.

So this lesson, you're going to need that replica account that you should have set up already with your parent or carer's permission.

You're going to need pen and paper, to make any notes that you might want to.

And also to try your best to remove as many distractions as possible so that you can really focus today.

Once you're ready, we can begin.

In this lesson, you will use the three control structures that we've been learning about in this unit, sequence, selection and iteration, to build a more complex programme.

And you'll also use Boolean variables, operators and expressions.

Let's take a look at this programme then.

It's all to do with guessing a lucky number.

And we had to go at this throughout the unit as we developed it, as we went to law.

So let's just look at those four sections.

We've got lucky = 13 and that's going to pick a lucky number.

Then we've got, can you guess my lucky number? And you've got guess = int and that's asking the user to guess.

And then you've got that section where it displays the feedback to the user.

And then you've got that final part, where it says goodbye to the user.

Which of these programmes segments will need to be repeated, if the user is allowed multiple guesses? Pause the video while you have a think about that.

It's going to be this section.

So this whole part here, it asks for the user to guess and it displays the feedback to the user.

So you're going to want to repeat those steps over and over again, if we need to ask it multiple times and give the user multiple attempts to guess.

So that would be the bit that needs repeating.

And what we're going to do is we're going to do some live coding and figure out how to do that.

Here's that programme then.

And we decided on those two sections that would need to be repeated again and again, while a user guessed.

So if I take a look at this, I've got my two sections here.

So that was the first section, where they inputted their number, their guess.

And then this was giving the feedback to the user.

So this whole section is going to need to go into a loop.

Now because I know it's going to go into a loop, I know that this whole section needs to be indented.

And our cheaty, quick way of doing that is actually to highlight the whole section and use the Tab key on your keyboard.

Because that moves the whole thing in, it'll tab the whole thing in, all in one go.

You can see me doing it there, you can see the whole thing tabbed over, which is a lot quicker than going down each one and tabbing it in.

So that's just a useful tip there, if you need it.

So now what I need to think about is the condition and what I'm going to do to make this while loop run.

And there's quite a few ways that you can do this but what I want to do is I want to create a Boolean variable.

So I'm going to make a Boolean variable and I'm going to call it guessed.

And because it's a Boolean variable, it can either hold a condition that could be evaluated as true or false or I can just simply write the words true or false in there.

And I'm going to put false because before the programme started, before anyone's managed to have a guess, then guessed is going to be false because they haven't guessed it correctly.

So it's going to start off being false.

Now I can start thinking about my condition.

So I've got my while loop now and I can set my condition.

So I can say, while guessed is not = to true, I want to run these bits of code.

So that means that while I haven't guessed, keep asking the question.

So while guessed is not = to true, I can get it to keep running that programme.

So at the moment, guessed is false.

When it runs that first time, that condition is going to be true because guessed isn't = to true.

And then as it goes through, as it loops, hopefully something's going to happen.

Let's test it and see what happens there, let's just have a look.

So I'm going to run my code.

So it says, can you guess my lucky number? Now I know it's 13 but I'm going to purposely get it wrong, so I'm going to put 76.

And it says, sorry it's not 76.

Can you get my lucky number? 32.

Sorry it's not 32, can you guess my lucky number? So you can see, we're stuck in a loop now at the moment but I haven't got it right.

It needs to be right for the loop to terminate, doesn't it? Let's get it right and see what happens, so 13.

Amazing, you guessed it.

Can you guess my lucky number? Hmm, something not right here.

So let's think about, let's go back to when a condition, when a loop, a while, when does it actually terminate? Can you remember when a while loop terminates? Have a think about it.

So a while loop terminates at that condition at the beginning.

So it'll keep checking, is guessed not = to true.

And it'll go through that block of code, while that is true.

And then it'll keep doing it and keep doing it and to keep doing it until guessed = false.

Sorry, until guessed is not = to true.

So what we need to do somewhere in our code, is actually make guessed = true.

So where are we in our programme do we know, for a fact, that guessed is going to be true? Have a look and see what you think.

Let's take a look.

So this is our block of code here, where it's going to be true because it says, amazing you guessed it, there.

We've got here, if guessed is not = to lucky, then print, sorry it's not.

Otherwise, then they're going to have got it right.

So if I added another line of code here, if I put another Boolean variable in there guessed = true, then now, when it goes to check the condition at the top again, it says, guessed not = to true.

While this time guessed is = to true, so this is going to be false and it's going to run it.

But let's just run the code and just see if that works now.

So I'm going to get it wrong and then I'm going to get it right.

And hopefully, the loop's going to terminate, let's see.

And it does, so it says, amazing, you guessed it.

And it goes to that final line of code there, line 16 and it says nice playing with you and the whole programme terminates at that point.

So that's that working.

What I want you to do now then is take a look at the worksheet.

It'll take that bit of code that we've started off there in our live coding demonstration and it'll take it even further.

So again, make sure that you're using the support on the worksheet to help you and that you're reading it carefully, that you're looking for syntax errors, that you're testing it often.

That you're using those worked examples to really make sure that you're trying to get the best out of this as you possibly can.

So I want you to complete the tasks in your worksheet, to build a more complex guess the number game.

Pause the video while you do that.

Fantastic, so how did you get on? Easy, challenging? Hopefully it wasn't too bad.

Maybe you got all the way, maybe you got almost there.

What I'm going to do is I'm going to go through the solution with you now in Python, so we can take a look.

So with programming, people don't always come to exactly the same solution in their code but it might work in a very, very similar way.

So you might have come to a slightly different solution to me and that is completely fine if it's working.

If it's doing what it needs to do, then you've done exceptionally well.

So don't worry if your code looks a little bit different to my code, that's okay.

As long as you try to meet the needs of the task that you've been asked to do.

So let's have a step through of this programme.

It's quite a big programme, isn't it? Now this is probably one of our biggest ones that we've looked at.

So we've now got the lucky number but also we've got a count variable as well.

Remember, we were looking at count variables and getting them to increment as programmes were happening within the loop.

So we've got here, guessed = false.

And we've got a slightly different condition here now.

So instead of saying, guessed not = to true, it's saying guessed = to false.

So while guessed is = to false, which will happen at the beginning and count is less than three.

Because you remember in the task sheet, it wanted you to make sure that they only had three guesses there.

So it's counting from zero and as soon as it gets to three and it's false as well, it's going to break from that loop.

Or if they've guessed three, if they've got less than three counts and they get it right, then that loop should still terminate as well.

So it says, that same block of code again that we've used already, which is to print what is your lucky number and they input it.

And then we've got this count now, this new variable and it increments.

They're on their first guess, so it's going to increment by one and then it's got this same bit of code again.

So if they've got it wrong, it's going to say, sorry, you're wrong.

Otherwise it's going to say, you're right.

And it's going to set that guessed variable to true, which is what we had before but we just didn't have that count there.

Once that loop breaks, it's providing some final feedback to the user as well.

So in the main part of the programme, it's saying, if guessed is = to true, it said, it took you the count.

It took you one guesses, two guesses, three guesses to get it right.

And if they got it wrong, it says my lucky number and it displays what the lucky number is.

And then everyone always gets that final bit at the end, nice playing with you.

So if you didn't quite get there or if you nearly got there or if you just want to explore this programme, you can see the link.

If you just rewind back, you can see that short link that's on your video view to have a look at.

And then you can explore this one and just see how it compares to yours.

It's always a good idea to have a look at people's code and explore it and read it and run it and just see how it actually works.

because that's really going to build your knowledge and your skills in your Python programming.

Congratulations, that is a superb effort.

We've got to Lesson 6 of your Introduction to Python Unit.

And hopefully you've made loads of progress and you understand now what sequence, selection and iteration is and you know how to incorporate them into some programmes.

So it would be really great to see if you've experimented with those control structures and maybe you've tried them with other programmes that you've thought about too.

So if you'd like to, please ask your parent or carer to share your work on Instagram, Facebook or Twitter, tagging @OakNational and #LearnwithOak.

Because we'd really love to see any of those experiments that you've done with your code or just the work that you've done in this lesson that I've set you.

So well done, you've worked extremely hard.

I'm very, very proud of you.

And I hope to see you soon for another unit.