Loading...
Hello, my name is Mrs. Holbrough and welcome to Computing.
I'm so pleased that you've decided to join me for this lesson.
We are going to use comparison and logic operators in Scratch to make a quiz.
Are you ready to make a start? Welcome to today's lesson from the unit Using Fundamental Programming Constructs in a Block-Based Language.
This lesson is called Operators and by the end of today's lesson you will be able to create complex conditions using comparison and logic operators.
You'll need to have access to Scratch for this lesson and it would be useful if you have your worksheet ready.
We will be using these keywords during today's lesson.
Comparison operator, comparison operator: an operator that is used to compare one value to another.
Condition, condition: used to control the path a programme follows.
Logic operator, logic operator: an operator used in a logical expression for example, AND, OR and NOT.
There are two parts to today's lesson.
You'll start by creating conditions using comparison operators then, you'll move on to creating conditions using logic operators.
Let's make a start with the first part of today's lesson by creating conditions using comparison operators.
This programme uses a comparison operator.
Can you see where it is? That's right, this condition compares the value of health to yes and then checks to see if they match.
If they do, it'll say, "Good, glad to hear it." If they don't, it will say, "Oh no, hope you feel better soon." Comparison operators compare the values of expressions.
This first one, score equals 10, uses the equals comparison operator to check if the score is equal to 10.
The next one, score less than 10 uses the less than comparison operator to check that the score is less than 10.
And the final one uses the more than or greater than comparison operator to check if the score is more than 10.
Time for a check, fill in the blank to correctly complete the sentence.
.
compare the values of expressions.
Pause the video here whilst you have a think.
That's right, well done, comparison operators compare the values of expressions.
Is this statement true or false? The card's value is equal to 5.
That's right, it's true.
The card is the 5 of hearts so it is equal to 5.
Is this statement true or false? The card's value is equal to 5.
That's right, it's false.
This is the 10 of spades so it's not equal to 5.
Is this statement true or false? The cards value is less than 5.
That's right, it's false.
The cards value is 10, so that's greater than 5.
Is this statement true or false? The card's value is more than 3.
That's right, it's true.
The card's value is 5 and that's more than 3.
Okay, it is time for your first activity of today's lesson.
Imagine that you run this programme three times.
What will the programme say if the year group variable is set to the following values each time? So A, set year group to 4, B, set year group to 6.
And C, set year to 8.
Pause the video here whilst you have a think and complete the activity.
How did you get on? Hopefully, you have these as your answers.
For A, set year group to 4, the programme would output Primary School Quiz.
For B, set year group to 6, the programme would output Primary School Quiz.
And for C set year group to 8, the programme would output Secondary School Quiz.
Okay, for the second part of Task A, you need to open the programme oak.
link/school-quiz.
You then, need to complete the condition by adding a comparison operator to the question 1 subroutine.
The programme should say "Correct," if the answer to question 1 is 4.
The code block has been started for you, but you'll notice that there is a missing part where you need to add the comparison operator.
Pause the video here whilst you complete the activity.
How did you get on? You're doing a fantastic job, well done.
So in that missing part in the if statement, you should have used a comparison operator with an equals.
You then needed to have the question 1 variable block and then in the answer bit you needed to have 4 'cause the correct answer for question 1 is 4.
If you haven't got that quite right, don't worry.
You can always pause the video here and correct your code.
Okay, we are now moving on to the second part of today's lesson where we're going to create conditions using logic operators.
Logical operators are used in logical expressions.
There are three main logical operators and, or and not.
And is true when both values are true.
Or is true when one or both values are true.
And not is true when the value is false, not true.
It's basically reversed.
Time to check your understanding.
Match the logical operator to the correct description.
Pause the video here whilst you complete the activity.
How did you get on? Let's go through the answers.
The and should be matched to true when both values are true.
The or is true when either or both values are true.
And the not is true when the value is false, well done.
Okay, back to our cards.
Is this statement true or false? The card's value is equal to 10 and the suit is equal to spades.
That's right, it's true.
The value of the card is 10 and the suit is spades.
Both conditions are true.
Is this statement true or false? The card's value is equal to 5 and the suit is equal to spades.
That's right, this one's false.
The cards value is equal to 5, but the suit is equal to hearts not spades.
So both conditions are not true.
Is this statement true or false? The card's value is equal to 5 or the suit is equal to spades.
That's right, this is true because we've got an or only one of our conditions has to be true.
So the value of the card is 5 so that condition is true, so the statement is true.
Is this statement true or false? The card's colour is not red.
That's right, it's false 'cause the card is red.
In Scratch, the logic operator blocks can be used to construct logical expressions.
They can also be combined with comparison operators.
So you can see here, we've got suit is equal to spades or suit is equal to clubs.
What are each of these conditions checking? The top one is checking that the number is greater than 30 and the country is equal to Spain, both must be true.
The bottom one is checking that the number is greater than 30 or the country is equal to Spain.
Either or both must be true.
Time to check your understanding.
In Scratch, which logical operator would you use to check if two values are both true? Pause your video whilst you have a think.
That's right, it's C the and logic operator.
Okay, it's time for your final activity of today's lesson.
Open the Scratch file oak.
link/school-quiz.
This is the same one that you used for Task A.
Complete the subroutine for question 2, which asks the user which river Paris stands on.
For Part A, the programme should say "Correct" if the user gets the answer correct.
For part B, the programme should say "Wrong," if the user gets the answer wrong.
And there's a hint for you, the correct answer is "Seine." Pause your video here whilst you complete your activity.
How did you get on with your code? You're doing a great job, well done.
So here's a sample answer.
We've got the subroutine for question 2.
It starts by asking, Paris stands on which river? It then sets a variable, q2, to the answer that the user is going to input.
It then has an if-else block.
Inside the if-else block, we have a condition that says is question 2 equal to Seine? If it is, then we are going to say "Correct." If it's not and the condition is not met, then we are going to say "Wrong." Remember, if you've got a bit stuck on this and you need some help, you can always pause the video here and correct your code.
Okay, you are now gonna complete the subroutine for question 3, which asks the user to name a country that borders France.
Pick one of the following: Germany, Spain, Russia, or England.
The programme should say "Correct" if the user gets the answer correct.
And the programme should say "Wrong" if the user gets the answer wrong.
Now this one has two possible correct answers.
So to get "Correct," the user needs to enter either Germany or Spain.
Pause your video here whilst you complete the activity.
How did you get on? You're doing a great job.
Here's a sample answer for part 3.
We've got a subroutine for question 3, which starts by asking the question, name a country that borders France.
Pick one of the following: Germany, Spain, Russia or England.
It then sets the q3 variable to answer.
We then have our if-else statement with our condition.
This time though, because there were two possible answers, we've used a logic operator, so we've got if q3 is equal to Germany or q3 is equal to Spain, then say "Correct." If that condition is not met, then it's going to say Wrong.
Remember, you can pause your video and correct your code if you need to.
You're now gonna build the final question for your quiz, question 4.
But unlike the previous questions, this question has two parts.
So for part A, you need to first ask the question, what do the U and S stand for in USA, for five seconds.
You're then going to break it down into two parts.
Ask what does the U stand for in USA? Then, ask what does the S stand for in USA? Store the user's answer for each question in a variable called q4-1 and q4-2 so you're going to need two variables.
And then create a condition that will check that the user has got both parts of the question correct.
If they have, display "Correct," if not, display "Wrong." So the user must get both parts of this question right for the programme to output correct.
Pause your video here whilst you have a go at the activity.
Great work.
Let's go through a sample answer for part 4.
So we've got our question 4 subroutine and we start by saying the question, what do the U and S stand for in the USA? We then break that down into the two parts and we ask what does the U stand for in USA and we store that value in a variable called q4-1.
We then ask what does the S stand for in USA and we store that value in a variable called q4-2.
We then have our if-else statement.
Inside that if statement, we have a condition using the and logic operator.
So we've got q4-1 equal to United and q4-2 equal to States.
If both of those return true, then it's going to say "Correct." If just one of those conditions is not true or if both of those conditions are not true, then it's going to say "Wrong." Remember, if you got a bit stuck, you can always pause the video here and correct your code.
Okay, one final part, if you've got a bit of extra time today, extend the quiz game by adding a score variable to add 1 to the score each time the user gets a question correct.
Pause your video here whilst you have a go at the activity.
How did you get on? Did you manage to create the score variable? Let's have a look at a sample answer.
So on the left-hand side we've got the main code block, which is controlled by the when green flag clicked event.
Underneath that, when green flag clicked, we're setting score to 0.
This is setting the score variable to zero at the start so that every time the user restarts the quiz, their score is set back to zero.
Now, each time the user gets an answer correct, we need to add 1 to their score.
So here I've taken the example of the q4 subroutine, but hopefully, you've done this in all of the questions.
So you can see underneath the if statement, so if they get the answer correct, we're changing score by 1 so this updates the score variable by 1 when the user gets the answer correct.
You've done a fantastic job today, so well done.
Let's summarise what we have learned.
Comparison operators can be used to compare values in a condition.
Logic operators and, or and not, can be used to build more complex conditions.
Well done for all your hard work today and I hope to see you again soon, bye.