Loading...
Hi, I'm Rebecca, your computer teacher, and well done for coming back to lesson three of this Introduction to Python unit.
Now, this lesson, you're going to need your Replit account which you should have already set up with your parents or carers permission, you're also going to need a pen and paper to make any notes that you might need to and to move any distractions out of the way that you possibly can.
Once you've done all of that, we can begin.
In this lesson, you will use selection or if statements to control the flow of programme execution between branches and you'll introduce elements of randomness into your programmes.
So far, the programmes that we've made have been executed line by line, and no decisions have been made there, it's just literally been one line after the other that has been executed.
But what if we wanted to do something different? So this programme at the moment says, "What's your name?" There's some input, and then it says, "Hello", and whatever they typed in it's going to say that on the screen back to them.
But what if you wanted the programme to recognise one particular name and treat it differently? At the moment, we don't know how to do that.
Let's have a look at the next one.
Here's one for a film, so we've got, "Best film ever?", they type it in and every single time it says, "That's, my favourite film too!".
But why, if it wasn't their favourite film or what if you wanted different things to be said based on what film they actually typed in? And then the final one, what if they typed in their year of birth and you wanted to give some tailored feedback, depending on what their actual age is? For this, what we need is something called selection, and this is when your programme checks conditions and select the path of action that they will follow accordingly.
And you might have seen conditions and selection before in Scratch.
These are the blocks that you have seen, so if some condition is met, then do these blocks of code.
And then you might've seen the if and else block as well.
So if it's true, then one thing happens and if it's false then another thing happens.
and we can do exactly the same thing in Python but we obviously just have to type the text in around it, the syntax, rather than dragging the block that we need.
So you will need an if or an if, else when there is more than one possible path for your programme to follow.
So we don't just want a programme to go, bam, bam, bam all the way through the whole programme, we want it to, based on a condition, run a certain piece of code or if it's false run another piece of code.
So we're going to look at selection in Python, and we're going to look at a greeting.
So we've all looked at this piece of code before, "What's your name?" then type in some input and it says, "Hello".
And what we going to do is, we're going to extend this programme so that depending on what the person writes in, it gives them different feedback, so let's have a look at that now.
So here is that programme in Replit now in our development environment.
Let's just execute it and just see what happens, see how it runs, so I'm going to press run and it says, "What's your name?" And I'm going to type in Rebecca, and at the moment, it just repeats back to me, "Hello Rebecca.", that's all it does.
So it's just taking that input and it's repeating it back to me.
But what I want to do is if I type in a certain word it gives me some feedback depending on the name that is been typed in, and I'm going to do this based on the scenario that Her Majesty Queen Elizabeth types in her name.
So if someone types in Elizabeth it's going to say, "Good morning your," or "Hello Your Majesty.", or "Good morning, Your Majesty.".
and if that's the feedback that's going to happen, but if anyone else types their name and who's not Elizabeth it's just going to repeat, but to them, Hello and then your name.
So we're going to need to keep all those lines of code that are already there because they're all really useful to us but we're going to have to add in some new code.
And if you remember from our example at the beginning we're going to need that if, we're going to need an else, and we're going to need the colons as well, you might not have noticed those but I'll show you those in a minute.
We're also going to need that condition too.
So then I've typed in that I've got there, "What's your name?" They'll have typed in their name and then we need to have a condition based on what they have typed in.
So start off with if, and then we have that condition.
Now, a condition can either be true or false, It cannot be anything else, if it is anything else, then you've written it wrong, okay? It has to be something that is either true or false when it's evaluated.
So let's think about what that condition needs to be.
So what variable are we going to need to use in that condition to find out if they've typed in Elizabeth, what do you think? We're going to have to use the user variable? So if user, and then we need to think of what needs to be true.
So we want the user to have Elizabeth held in that variable, and if that's true then we want a certain bit of code to run.
So what we've got to do is we've got to include something called is equal to and in Python we do this by doing two equal signs.
So if you look the one above, line two on user, it's just got one equal sign and that's because that's for assignment, but if we're doing a condition and we're checking if something's true, then we would do two equal signs because that means is equal to, and then we want it to be equal to Elizabeth.
Now because Elizabeth is going to be string, so remember that input function always returns a string, unless we wrap around integer around it or something else.
So we want it to check string, so we're going to put Elizabeth in those speech marks like so and then we just need to remember that colon at the end.
So this is just important syntax.
So you might forget that and you'll get a syntax error, and again it's totally normal, you're going to get syntax errors, but if you do, it's probably because you've missed that colon or something else like that.
So if user is equal to Elizabeth we wanted to print, and we know this line of this functional already, So print, we want it to say "Good.", whoops, "Good morning Your Majesty", Okay.
Now at the moment, we've just got there a selection statement.
So as I always say, it's really important to test your programmes.
So I haven't finished yet, but I want to test that it works and see what it actually does.
So I'm going to run my programme and just see if I've made any syntax errors at this point.
So here we go saying, "What's your name?" just like last time.
I'm going to put in Elizabeth, 'cause I want to test that that bit of code works, so Elizabeth, and then I'm going to press the Enter key, and now it said to me, "Good morning Your Majesty", but it's also said, "Hello Elizabeth" underneath.
And the reason it's done that it's because it's run every line of code.
This was true because I typed in Elizabeth, so the block of code there that was indented within that if statement has been executed, but also because this is on the main line of code on the main part of our programme, and it's all the way here it's going to run that piece of code as well.
So we don't want that to happen, We want it to only run this line of code if this condition is false.
So now we need to have our else, so now we put in and, this is where another syntax error might happen, 'cause if you look, did you see what happened to my cursor when I press the Enter key? if you look you press the Enter key and it automatically indents it because it thinks you want to write more blocks of code within that if statement.
So this is important thing 'cause sometimes people start writing else there and that isn't going to work because it's going to be within the wrong block of code, so I've to actually press Backspace.
Let's just look at that again, press the Enter, Backspace, and then I do my else, and then, so now I'm saying, "Otherwise run this line of code." And again, I haven't got it indented now so still going to not do it properly, so I've got to make sure this is indented and you can do it by pressing the Tab key, or sometimes I like to press Back and press Enter just to double check that is definitely in the right place.
So us run it now.
Here we go.
So it's saying, "What's your name?" I'm going to put Elizabeth, and just check that bit of code works.
So Elizabeth, Good morning Your Majesty and that other bit hasn't run now, so there's obviously something working there.
So let's just type Rebecca and see what it does and hopefully it doesn't say, "Good morning Your Majesty".
It just says, "Hello Rebecca".
So now we've got that selection statement, and what he's doing is based on the condition it's running that block of code but it's not running both blocks of code and that's what's really, really important here.
It's making the decision, so once that condition is true it's running one block of code, but if it's false, it's either going to ignore it and go to the next line or it's going to run the else statement So within our condition there we saw one comparison operator, which was just equal to, so that's the first one on the list there, but there's lots of other comparison operators that you can use in Python, and don't worry, I will remind you of them in your worksheets if you need them, but these are the ones that you could use.
So you've got equal to, not equal to, less than, more than, less than or equal to, greater than or equal to.
And you've probably seen something similar to this, in class before, maybe not the exact same symbols, but you've probably seen the words before and you can make a bit of a connection there.
So these are all to do with conditions and when those conditions are evaluated they will either be true or they'll be false.
What I'd like you to do now is pause the video and have a go doing that selection statement yourself.
So if you need to go back on the video then just do that and copy me if you want to, if you want to try and do it from memory as well you can try that too, but pause the video now while you have a go.
You're going to be having a go of this worksheet next, and I just want to talk you through it a little bit before we get started.
Now, this worksheet starts off with some worked examples.
So if you take a look at that first one and that is the exact example that you have just done in your live coding practise.
So this exact same one, and you can use this to help you answer the questions that are on the worksheets.
So you've got your work example at the beginning, and then you've got a syntax checklist.
Remember you're going to get syntax errors, and this checklist will help you try and fix those errors for you.
So you can go down this checklist to make sure that you've done everything correctly to help you fix the errors in your code.
So if you've got loads of errors, come back to this sheet here and take a look at it and go through the checklist and that will help you fix those errors.
So it's really, really important, that's why it's there.
And then the third one is just talking about testing your programme.
I mentioned it a little bit in the live coding activity that it's important to test often so that you can try and spot those syntax errors early on.
And it's really to make sure that you're testing throughout.
So every single task test it, so there's some advice there on testing, and then this is the actual task.
So the task is you're going to do that film critic one.
So remember that second example that we had at the very beginning? We're going to turn that film critic little programme into a much larger programme that now gives some feedback.
So it says, "You're going to make a programme "that asks a user's favourite film.
"The programme will either react enthusiastically "to one particular film or display a generic comment." And this is something that I'm going to use quite often with here, which is example input and output.
So if you take a look on there you've got what will be happening on the left and then the output that will be displayed.
On the right-hand side it says, "The programme displays a prompt "and waits for keyboard input." So Best film ever? and then you've got that input prompt underneath it and then the user types in their reply, so Star Wars and then the programme displays the results, "Star Wars is not too bad".
So you're going to have that selection statement that takes that condition, if the film is equal to Star Wars then provide this bit of feedback.
And then you've got another example if they type BFG, so an else may be, or maybe another if, it's up to you.
If they type BFG it types, "BFG is my favourite too".
So think about how you might want that programme to actually work and see if you can get the output to be replicated.
So these here can be used to help you test your programme as well.
So it's not just about providing like support on how to do it step by step, it's about giving you tools so that you can test your programmes effectively too.
So these are really important.
And then now is the actual steps to get started, okay? So this worksheet seems really, really long and overwhelming but actually it's been done like that to support you.
So it's just about reading those worksheets really carefully.
So then you get to step one, and this is your actual instruction now.
So all that was all just information to help you and now this is your instruction, so step one, open this incomplete programme using that short link, and I've already shown you how to use short links so that bit there ncce.
io/py-critic-30, you just type that into your web browser and it'll take you to Replit and you should be able to edit that in your account.
And then it gives you some start code, and what you've got to do is start making sure that it works properly.
So if you look on here, the start code isn't quite right.
So step one is literally just open the programme and then step two, complete line three.
So go back line three has got a condition on it, that's blank, so you've got to fill it in, and then step three indent any lines of code.
So have a look back, there's no indents there at all, and hopefully know by now that if statements need indentation, so you're going to have to add that in somewhere.
And then once you manage to run your programme successfully, test it.
So it's just important there that you read the instructions really carefully and you just understand how this worksheet works, 'cause it seems really long but actually it's just a few steps and then some stuff to help you as well.
So what you're going to do next is you're going to have take a look at the actual programme and the worksheet and do it yourself.
There is also a lucky number programme on there as well for you to have it go at to.
So what I want you to do now then is take a look at that worksheet and follow the instructions for the film critique and the lucky number exercises.
Pause the video and come back when you're done.
How did it get on? Can you give me thumbs up, thumbs down, somewhere in the middle.
So when we say these should be a little bit challenging but not so hard that you can't do them.
So hopefully there was enough support there on that worksheet to help you out.
If you were struggling and you needed to go fix your syntax error or you weren't quite sure so you went back to the worked example and you saw how that was written, so hopefully you went back and you persevered with it and you managed to do it.
So that first one, the solution was this one here.
So take a look.
Did you have something like that? I mean, on this one, you've got film not equal to BFG.
You might not have done that, you might've done it the other way around but you could have done it this way with the not equal to sign, and maybe you had to even rewind the video to find out that not equal to symbol, just to see what it actually was 'cause you might not have remembered.
So hopefully you've had a go at that and you've got that right.
And if you didn't, you could just pause the video now and go and try it, that'd be a good thing to do as well if you didn't quite get there.
So let's have a look at the next one.
So the next one was that lucky number one and changing that programme.
So take a look at this.
We've got the Guess the number, we've got if guess is equal to lucky it's going to print, "Amazing, you guessed it", otherwise, it's going to say, "Sorry, you didn't guess it", and "My lucky number is" and it will reveal it.
So you might have had something like that something similar and again, as long as it works and that's great and that you tested it and it worked and that's brilliant.
You might not have exactly the same that I've got there but you might have something similar.
But again, if you didn't quite get there maybe go and pause the video now and just try it and see if you can get it to work.
It's all about practise and persevering this season and not getting too worried if you didn't quite get there 'cause there's always going to be these solutions for you to go and try yourself anyway.
So let's have a more, in-depth look at this programme here.
So we've got four, essentially four sections to this programme, and some look at the first one.
The first one is just picking the lucky number, so we've got a variable and inside that is held the number 13.
You've then got prompt the user to make a guess, and that's the instructions using that print function and then we've got the variable guess and the integer input there, can you see our he's got integer wrapped around that input because input returns a string and then integer then returns it as an integer, so that's really, really important when you need an integer.
And then we've got if guess is equal to lucky, it's going to print, "Amazing, you guessed it".
So you've got, it's checking the answer and it's providing some feedback.
And then finally, we've got that say goodbye, that final statement that will run for everyone.
So if we take a look at that selection statement the bit when the condition is true that will only run if it's true, and then the else, that indented bit will only run if that condition is false, but that final line of code will run for everyone because it's all the way back, it's all the way back, isn't it? It's not indented inside a selection statement, it's just in the main programme, so that's going to run.
So executing the programme works like this, and at the beginning we've got the state of lucky is 13 and if somebody types in 13, so then lucky is going to be equal to guess.
So let's have a look.
So guess is equal to lucky, so the condition evaluates as true.
And then, because it's true that line of code runs print amazing, you guessed it, and the output is Amazing, you guessed it.
And then that final line of code runs print nice playing with you.
So that's what happens when it's true, so let's look at the other option.
So this time the state of lucky is 13 and the user has entered seven.
So this time guess is equal to lucky it's false, so the line that's within the indent doesn't get executed, it goes straight to the else and this time it prints, sorry, it's not guess or what have they typed in, so seven, and then my lucky number is 13, so whatever's held in 13.
But again it still runs up final line of code because that final line of code isn't indented, it's in the main part of the programme.
We're going to take this lucky number programme a little bit further now, because what if you wanted a programme that didn't always pick the same lucky number? This is where we can actually introduce randomness into our programmes.
So I'm going to show you how to do this now in Replit In order to introduce randomness to our programmes we have to actually import something called a module and also import a function from that module into our programme.
Python has got all sorts of libraries of code that we can tap into that we need to.
So that we don't have to write them all ourselves, we can tap into pre-built code that we can use, and they're in libraries and we import them into our programmes.
So I'm going to show you how to do that now, I'm going to go from the module, whoops not from module, from the module random, so the module is called random, I'm going to input the function randint, okay? Now these input statements are always at the top of your programme, you might want to even have some space as well between them too, just so that you can really separate them but you always have them at the very beginning of your programme.
So I'm saying from the random module I want you to import the function randint, and randint is a function that will select a random integer based on the arguments that you have given it.
So I've got there at the moment, "lucky equals 13".
But what I want to do instead is not have 13 there, I want it to return a random number, so I'm going to use that function.
So randint, and randint has two arguments and you can see this actually pops up when I start typing it in there.
We've randint and you've got the first argument there and the second argument there, so it takes two arguments.
The first one is the lower value of the range and the second one is the upper value of the range.
So I'm going to do one to 20, so one to 20, and it's already put that bracket in for me so I don't need to type it in myself.
So now what is going to do is it's going to select a random integer between one and 20 for me to guess.
So now I'm going to run the programme and see what actually happens, so let's run it.
So we've got "Guess, my number", so I'm going to put three, "Sorry.
it's not 3" "My lucky number is 19".
"Nice playing with you", okay.
Now, if I run it again that function is going to run again.
So it's not going to be 19 the next time, it could be 'cause it's random, but it's unlikely.
It's going to actually run that function again and return another random number between one and 20.
Now, if I wanted to test this programme just to make sure it works what I could do is underneath is this I could print lucky just so that I knew what was being held in that variable so that I could test it.
So I'm going to do that now, I'm going to run it and it's going to say seven on the screen, so now I know the lucky number is seven so I can cheat a little bit and just test it, so seven.
And it says, "Nice playing with you", it says, "Amazing, you guessed it, nice playing with you".
So it has actually run it properly.
So when you're just testing your programmes you can actually print the variable just so that you can make sure that the both options works, and now if I run it again, I'm going to run it again and I've got, so the answer this time is 13 but I'm going to put four just so that I can test my programme and make sure it does the opposite, so I'm going to press the Enter key, "Sorry, it's not 4, "My lucky number is 13, "Nice playing with you".
So it's always important to print variables, so you can test things as well to make it a little bit quicker especially when you're using randomization because you have no idea what's going to be selected there returned by the function, so it's good to do that.
And it's also a good idea to make sure you test both options, so whether if it's true and if it's false make sure you've tested both sides.
Pause the video now and have a go at that yourself.
Remember you can always rewind and see what the code was if you forget.
Fantastic, right, what we're going to do now is we're going to have a look at some questions and we're going to read the code, I'm going to read the question and we're going to try and figure out what the answer is.
So you've got their question, "You are making a dice rolling game of filling the gap "to complete the programme." So you've got there "from random input randint", so the same input statement that we've already used.
You've got variable diceroll, something needs to happen in that assignment, something needs to happen there in order for a random number to be put there.
And it says, "You rolled a", and then it says, "diceroll".
So what needs to be there? Which line of code are we going to use? We just used it for a random number for our guessing game which line of code was it? Have a think.
Let's look at the answer, so it was that final line, wasn't it? randint one to six, so that's going to call the function randint and it's going to return a random integer that's between the range one and six, so well-done there if you got that right? Let's look at the next one then.
So when this programme is executed, what will his output be? So take a look at those answers, pause the video if you need to, and we'll look at the answer in a few seconds.
Okay, let's have a look at the answer then.
So, "You rolled a", it's definitely going to say that because we've got that in that print statement and then diceroll is going to print whatever is been held in that variable diceroll and that's going to be the random integer ranging from one to six.
So what I want you to do now is take a look at this programme here, and I want you to type it into Replit, run it a few times to see what changes each time, okay? So pause the video now while you have a go at that.
Brilliant, so let's look at some subtle points to do with randomness.
So, "Whenever this programme is executed "it will produce the same output", do you think that's true or do you think that's false? Take a look at that programme, pause if you need to.
It was false, because it's generating a random number, it could be different every single time and we can't really predict exactly what that number is going to be.
So let's have a look at this one then, "True or false, the instruction highlighted "will always be executed." So this is thinking about back to that selection, now, do you think that's true or do you think that's false? It is false.
It's inside that else statement, isn't it? It's being indented, so we'll only run if that condition diceroll more than three is false.
So it's not all the way over to the left in the main programme, so it's not only going to run if that condition is false.
Brilliant, we've got to the end.
Hopefully, as I always say, you found it a little bit challenging but not too challenging, and that you use that support that was there for you as well, if you were struggling, 'cause that's what it's there for.
And the important thing is I just want you to keep trying and keep persevering with this and fixing those syntax errors and trying to get help where you need to use the resources that you've got.
So hopefully you've learned a lot in this lesson and you've got something to show you for it, and if you have, if you'd like to please ask your parent or care to share your work on Instagram, Facebook or Twitter, tagging @OakNational and #Learnwithoak, 'cause as always, we'd love to see what you've been up to this lesson and I'll see you again soon for another lesson.