Loading...
Hello, my name is Mrs. Holborow, and welcome to Computing.
I'm so pleased that you've decided to join me for today's lesson.
Today, we are going to be looking at logical operators and how we can use logical operators in selection statements.
Welcome to today's lesson from the unit, Programming: Selection.
This lesson is called Logical operators.
And by the end of today's lesson, you'll be able to use logical operators to build more complex selection statements.
Shall we make a start? We will be exploring these keywords in today's lesson.
Logical expression.
Logical expression, an expression that combines relational and logical operators.
Selection.
Selection, used when there is more than one possible path for a programme to follow.
Look out for these keywords throughout today's lesson.
Today's lesson is split into two parts.
We'll start by using logical operators to compare values.
We'll then move on to use logical operators in selection statements.
Let's make a start by using logical operators to compare values.
Arithmetic expressions evaluate to a number.
You can see here on line 1, we have number = 30 + 3.
And then on line two, we are printing out the value held by the variable number.
And that returns 33, because 30 plus 3 is equal to 33.
Logical expressions, however, evaluate either true or false.
So, here, we are initiating the variable score to 20 on line 1.
We then have the variable called you_won, and that's equal to score > 30.
And then we are printing out the value held by you_won on line 3.
Now this is gonna return false because score is not greater than 30.
Logical operators are used when you need to compare multiple values with relational operators to return a single value of true or false.
The and operator is true when both relational operators are true.
The or operator is true when either or both relational operators are true.
And the not operator is true when the relational operator is false, i.
e.
not true.
Time to check your understanding.
Which logical operator is true when either or both relational operators are true? Is it A, and, B, or, or C, not? Pause the video whilst you have a think.
That's right, the correct answer is or.
The or logical operator is true when either or both relational operators are true.
Which logical operator is true when both relational operators are true.
Is it A, and, B, or, or C, not? Pause the video whilst you have a think.
That's right.
The correct answer is A, and.
The and operator is true when both relational operators are true.
Relational operators can be used to compare two values within a condition.
So, here, we have adult_ticket = age > 12.
So, we are comparing the values held by age and the value, 12.
Logical operators can be used alongside relational operators to build logical expressions where more values need to be compared.
So, here, we now have adult_ticket = age > 12 and age < 60.
A playing card has three attributes.
The colour, which is either black or red, the suit which is hearts, clubs, diamonds, or spades, and the value, so the number that's on the card or jack, queen, king, ace.
In the following slides, you'll be shown a playing card and an expression.
Decide if the card evaluates to true or false.
So, here, we have the five of hearts and we have value > 6.
Does this evaluate to true or false? That's right.
It evaluates to false.
The cards value is five, which is not greater than six.
Okay, we still have the five of hearts, but this time the expression has changed.
This time the expression reads, value == "king" and suit == "hearts".
Does this expression evaluate to true or false? That's right.
This expression returns false.
The value is not equal to king, but the suit is equal to hearts.
Remember, with and, both conditions must be true.
Okay, let's have a look at the next one.
This time we have the 10 of spades.
The statement is suit != hearts.
Will this return true or false? That's right.
It'll return true.
The suit is not hearts, it's spades.
Here, we still have the 10 of spades, but the statement this time is value > 7 or suit == "spades".
Will this return true or false? That's right.
This will evaluate to true.
Although the value is not less than seven, it's 10, the suit is spades, and here we are using or, so either one or both of these conditions can be true to return True.
Okay, we are now moving on to our first set of tasks for today's lesson.
Complete the table to compare values using logical operators.
So, you have three cards, three logical operators, and you just need to put true or false, or T or F, in the column at the end of the table.
Pause the video whilst you have a go at the activity.
How did you get on? Let's have a look at the answers together.
So, the first one, the card was the five of hearts.
The logical operator was value == 5 and suit == "hearts".
Both of these are true so it's going to return true.
The next card was the 10 of spades, value == 10 and suit == "hearts".
So, the same logical operator as we had before.
This time, it's false, because although the value is equal to 10, the suit is not equal to hearts.
Last card was the nine of diamonds, value == 9 or suit == "hearts".
This time it's true because we are using the or operator.
For part 2, build a logical expression to check if a playing card has a value less than or equal to 10 and is of the suit hearts.
For part 3, which of the following cards would return true for the logical expression you've just written for part 2? Pause the video whilst you have a go at the activity.
How did you get on? Hopefully your logical expression looks similar to this.
Value <= 10 and suit == "hearts".
You were then asked, which following cards would return true for the logical expression? The 10 of spades would not return true.
The five of hearts would.
The nine of diamonds would not return true, but the 10 of hearts would.
Remember, the value is less than or equal to 10.
So, 10 is equal to 10.
Okay, we are moving on to the second part of today's lesson and you are doing a fantastic job so far.
So, well done.
We are now going to use logical operators in selection statements.
Logical operators can be used in selection statements to control programme flow.
So, here we have a snippet of code, which says line 1, value = int(input()).
So, take a number from the user.
On line 2, if that value is greater than 6, we are going to print "My card as a match", else, we are going to print "My card is not a match".
The if statement checks if the value of the card is greater than 6.
What would be displayed when this code is run with the card the five of hearts? That's right.
The code would display, "My card is not a match." That's because the selection statement on line 3 returns false.
The value is not king.
So, the selection statement, which is value == "king" and suit == "hearts" returns false.
So, the else statement is run and the print statement, "My card is not a match," is run.
What will be displayed when this code is run? This time, the card is the 10 of spades.
Maybe pause the video and have a think.
That's right, "My card is a match," will be printed.
That's because the selection statement on line 2, if suit != "hearts", returns true.
What will be displayed when this code is run? We still have the card, the 10 of spades.
Maybe pause the video and have a think.
That's right.
"My card is a match" will be returned.
That's because the selection statement on line 3, if value > 7 or the suit == "spades", returns true.
Time to check your understanding.
Can you spot five errors in this programme? Pause the video whilst you look carefully at the code.
Did you manage to spot them all? Let's see if we can go through them together.
So, line 1, there should only be a single = here because we are assigning the value input to colour, we are not doing a comparison.
On line 3, the word, And, should be in lowercase.
Also on line 3, the == is required here 'cause we are doing the comparison.
So, it would be suit == "hearts".
On line 5, the E in the Else statement should be lowercase.
And on line 6, we are missing a final bracket for our print statement.
Hopefully you manage to spot all five of those errors.
Well done.
Now, rearrange these lines of code so that the programme will ask for a password.
Check that password against the stored password.
If it matches the stored password, it will output "Access granted." If it doesn't match, it will output "Access denied." You have all the correct lines of code, they're just in the incorrect order.
Pause the video whilst you have a think.
How did you get on? Did you manage to put the code in the correct order? Well done.
Line 1 should be stored_password = "Fish4321" in speech marks.
Line 2 should be print("Enter password:").
Line 3 should be password = input().
Line 4 should be if password == stored_password:.
Line 5 should be print("Access granted").
Line 6 should be else:.
And then line 7 should be print("Access denied").
Variable initialization and assignment should execute at the top of code where possible.
So, you can see here, we are assigning the value, "Fish4321", to the variable stored_password at the very top of the code.
The user will then need a prompt to enter the password before they enter it.
The user input will need to be held in another variable.
In this case, we've called that variable password.
And if statement is used to check if the stored password matches the entered password.
If the condition is true, then it will output "Access granted".
If the condition is false, it will output "Access denied".
Okay, we're moving on to the second task of today's lesson, and you're doing a fantastic job so far.
So, well done.
I'd like you to improve the sandwich ordering programme, which can be accessed from oak.
link/sandwich-order-improved to set different prices for meat and vegetable fillings.
The prices for the sandwiches are as follows.
So, a sandwich with meat filling is 2.
80 pounds.
A sandwich with vegetable filling is 2.
50 pounds.
A wrap with meat filling is 3 pounds.
A wrap with veg filling is 2.
70 pounds.
To add crisps to a sandwich or wrap, you need to add 30p.
Add selection statements with logical expressions to calculate the correct price for the order and then display the total to the user.
Pause the video whilst you have a go at creating the programme.
How did you get on? I'm sure you managed to create your programme.
So, well done.
Let's have a look at some sample code.
If you want to see the whole solution here, you can go to oak.
link/sandwich-order-improved-solution.
I'm just gonna show you the first part of code here and then we'll move on to the next slide to see the next part.
So, we have lines 1 to 10, which are the print statements to tell the use of the prices of the sandwiches and wraps.
On line 11, we are initiating this variable, total, and setting it to the value, 0.
On line 12, we ask the user if they would like a sandwich or a wrap, and we are storing that choice as input.
On line 14, we are asking the user if they'd like meat or veg filling, which we're asking them to put an m or a v, and we are storing that input as filling.
So, here's the programme continued.
So, online 16, we have if choice == "sandwich" and filling == "m", then total is going to be equal to total plus 2.
80 pounds.
Elif choice == "sandwich" and filling "v", or veg, then the total is going to be equal to total plus 2.
50 pounds.
And then we have another elif which says if choice == "wrap" and filling == "m", or meat, then total is going to be equal to total plus 3 pounds.
We then have an else statement, which is going to be for the veg wrap, 'cause that's the only option remaining.
So, we're gonna have total = total + 2.
70 pounds.
You may have done your selection statements a slightly different way round, but that's absolutely fine.
On line 24, we are asking the user if they want to add crisps to their order and storing that as an variable crisps.
If crisps is == "y", then on line 27, we are saying total = total + 0.
30.
We're adding 30p to the total.
On line 28, we are using the round function to round the variable total to two decimal places.
Don't worry if you haven't done this in your programme.
And then on line 28, we are finally printing out the total of the bill to the user.
Remember, again, if you want to go and see the full solution, you can do that by visiting oak.
link/sandwich-order-improved-solution.
Okay, we've come to the end of today's lesson, and you've done a fantastic job.
So, well done.
Let's summarise what we've learned together.
Logical operators are often used when you need to evaluate multiple relational expressions to return a single value.
Logical expressions return as either true or false.
And the three basic logical operators are and, or, and not.
Logical expressions can be used in selection statements.
I hope you've enjoyed today's lesson and I hope you'll join me again soon.
Bye.