video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher for the Programming Part Two unit.

For this lesson, you're going to need your Repl.

it account, which you should have already setup with your parent or carer's permission.

It's also a really good idea to have a pen and paper handy to make any notes or to answer any of the questions that I give you in this lesson.

And the most important thing, try and remove as many distractions as you can so that you can really, really focus in this lesson.

Once you've got all of that ready, we can begin.

In this lesson, you will describe how Boolean or logical operators can be used in expressions.

You'll walk through code that uses conditions with Boolean or logical expressions.

And you'll write and use expressions that use Boolean or logical operators.

For the first activity, you're going to be given a Parson's puzzle.

And this is where you are given all of the lines of code to solve a problem, but they are in the wrong order.

Your job is to take those lines of code and place them in the correct order.

So pause the video now, while you look on your worksheet, and see if you can solve that Parsons puzzle.

Excellent, let's look at the solution then.

So here's the answer.

First of all, you have variable initialization and assignment.

It should execute at the top of the code where possible.

The user will then need a prompt to enter a password before they enter it.

And the user input will need to be held in another variable.

An if statement is then 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, then it will output "access denied." Right, let's think about these playing cards, and you've also got a worksheet with a playing card on.

So you've been given a playing card, the five of spades, and this is also available on your worksheet if you wish to use it, you need to know the following properties of your card.

So colour, black or red.

Is it black or red? It's black.

The suit, if you're not sure, the suit is actually spades.

And the value on this card is a five.

You will now be shown some logical expressions.

You have to decide if your card evaluate as true or false.

If you are able to print your card, then you can hold up your card when required.

Alternatively, you can draw a quick sketch of your card to hold up.

Let's get started then.

So remember, you're five of spades, which is a black suit as well, okay? So value you greater than six.

So hold up your card if you think that's true or keep it down if you think it's false.

It's false so you're going to want to keep it down.

Okay, so value is equal to King and suit is equal to hearts.

Is that true or false for your card? It's false, isn't it? Because we haven't got a King, we've got a five.

And we haven't got a suit of hearts.

we've got the spades.

So "suit is equal to spades "and suit is equal to hearts." I think that's a trick question, isn't it? Is that possible? Can you have a card that's equal to spades and hearts? No, you can't.

So it's going to be, everyone's going to keep their car down at this point.

And then now, we've got "suit not equal to hearts." So is that our card? Yeah, so you should be showing up your card now because the spades, the five of spades, is not equal to hearts so that means it's true.

So we'd say my card is a match.

Now, we've got "colour not equal to red." Okay, so that one's true because ours is black.

And" suit is equal to hearts." Is that true? No, it's not true, is it? So we keep our card down.

We don't put it up.

Okay, this one.

Oh, we've got an "or" this time.

So "value less than seven.

." Is our value less than seven? Yes.

Five, isn't it? So it's less than seven.

"Or suit is equal to spades." Now, both of those conditions are true for ours, aren't they? So we've got spades and it's less than five.

So for this one, we're definitely holding our card because it's true.

My card is a match.

Now, keeping that in mind, how those Boolean expressions actually work, you're going to do some investigation and you're going to be looking at a sandwich order calculator.

And you're going to use the sandwich order calculator section of your worksheet to investigate and modify a programme.

And in, that you'll see some more code that uses those logical operators.

So pause the video while you have a go at that.

Excellent! So what I'm going to do now is I'm going to go through the solutions with you and just explain that code a little bit more, to either help you tick off that you've got it right or to give you a bit of a support, if you are struggling with some of those questions.

So let's take a look in Repl.

it and see what I am going to do.

So the very first thing that you would have been asked to do would be to make a prediction about this programme.

So you should have had a really good look and a read through, see which bits of code that you recognise, that you've got if statements, statements in there.

You've got print, you've got input.

There should be quite a lot of things on there that you recognise and then make a bit of a prediction about what's going to actually happen when this executes.

They need you run the programme and seen if your prediction was correct, and then thought about, "well, did anything surprise you about that? "Were you right?" "Were you wrong?" And all that kind of thing.

And again, just like I said in last lesson, don't worry if your predictions were incorrect or slightly off, that's completely normal.

We're all learning at the moment.

So this is all new to you.

So you should be getting a little bit better at reading these programmes and making predictions but you're still not going to be 100% perfect.

So don't feel bad about getting that a little bit wrong because it's completely fine.

Now, I'm going to go through these investigate tasks with you.

So step one was which bread type do you need to choose for the total cost to increase by three Pounds? So I need to figure out where the bread type actually is.

So we've got "sandwich or wrap" and they type "bread type" in here, so at line four, but where is it that it does it for three? So we've got there, "If bread type not equal to sandwich, "then they're charged two Pounds.

"Otherwise, they're charged three Pounds." So the question was, "which bread type do you need to choose "for the total cost to increase by three Pounds?" So if it's not equal to sandwich and they're two Pound, so that means wrap is two Pounds.

So if it is equal to sandwich, then its three Pounds.

So the answer to there was sandwich.

And they say, "which filling types can you choose "for the total cost to increase by a Pound?" So where's the filling types? So we've got here, "meat, vegetarian, or vegan." We've got filling type there.

So now, somewhere along this line, we shall have "filling type." So there we go.

So if filling type, not Philip, "filling type is equal to vegetarian, "or the filling type is equal to vegan." Then it's going to add a Pound.

Otherwise, it's going to add a Pound 50.

so it says which filling types can you choose for the total cost to increase by a Pound so they can choose vegetarian or vegan.

That would be the answer there.

And it says, "Which choices lead "to sugar tax being applied? So it starts off where you've got a variable sugar tax equals 50 pence.

And then, you've got things like fizzy drink and cookie and stuff like that, but where is sugar tax? Ah, here we go.

So we've got no sugar tax there but if we go to pudding.

So if pudding equal is equal to cookie, and drink is equal to fizzy drink, then it adds a sugar tax there.

So if they've selected a cookie and a fizzy drink, then the sugar tax is applied.

And then, step four says, "List the two possible choices "that you can make to get 50 pence taken off your total." Oh gosh, let's have a good look.

So there's nothing there.

So where's minus 50 pence? So we've got minus 50 pence here, right? Okay, so if "pudding is equal knowledge," if they choose to not have a pudding, or if they choose to not have a drink then, then they're going to get 50 Pence taken off their total cost.

So that's the answer there.

And then step five, "On line 21, change the or to an and.

"What choices online do you now need to make "to get 50% off?" So if I change this "or" to an "and," I need to now have pudding as none and drink as none in order to get 50 pence off.

So that's a bit of a calm there, isn't it.

I'm going to have it back as.

So then we have some modified tasks for you to have a go at.

So the first one was the code only works if you enter the data in lower case, modify the code so that it converts the input to lowercase automatically.

So if we just double-check this.

Yeah, it is all in lower cased, isn't it? So anywhere I've got an input, I need to put that dot lower, which is what we've learned about over the last few lessons.

So dot lower, dot lower, dot lower.

Got any more inputs? No, we haven't.

And I'm going to actually test the code 'cause I've made a change there.

So I'm going to put "sandwich" in capital letters.

And I'm going to put "meat." And I'm going to put "cookie." And I'm going to put "fizzy drink." Your total is five Pounds.

So he's still working and I've put in all capital letters, so I know my code is working.

And then he says, number two, "add in an option for if they would like an extra sauce." She says, "Look at the original lines, "three and four for sample code." So we've got "print sandwich or wrap" and then we've got "bread type equals input dot lower." So there's a sample code to help me with the question.

And it says, "add in an option "for if they would like an extra sauce." So I'm going to put this down here, I think.

So print, "would you like an extra sauce?" And then I'm going to have a sauce.

That's not you spell "sauce." Sauce equals input dot lower.

And I'm actually going to put my own brackets, "Yes or no" So that they know what to type, okay? And then you've got, add in an option for if they would like an extra salad.

So I'm going to put in another one now.

So print, "would you like an extra salad?" And then put "yes or no." And then I'm going to put salad equals input dot lower, like so, okay.

And then add in some code to increase the cost by one, if they choose an extra sauce and an extra salad.

So I had something similar to that down the bottom, didn't i? I've got here.

"if putting is equal to none "or drink is equal to nine." And then, I've also got "cookie and drink." So I've got some code that I can look up there.

So I'm going to put it here.

So if we've got sauce is equal to.

Was it lowercase? Yup.

Yes, oops.

And which one was the other one? "Salad equal to yes," then I've got to increase it by a Pound.

That's right, wasn't it? Adding some code to increase the total cost by a Pound if they choose an extra sauce and an extra salad.

So now I have total cost equals total cost plus 1.

00.

adding a Pound on.

Really, I should be testing my code as well at that point.

So I'm going to run my programme and I'm going to put sandwich, meat, cookie.

She's making me hungry, this is.

Fizzy drink.

"Would you like extra sauce?" "Yes." "Would you like extra salad?" "Yes." And it's six Pounds.

So it's all working, no syntax errors and it's calculating it correctly.

So I've done my modification steps as well.

You're now going to have a look at making your own pizza calculator.

Now the code that you use for this problem is going to be very similar to that sandwich calculator that we just created.

So do keep that code somewhere handy so that you can refer back to it when you're doing this pizza calculator programme.

And remember, just like we had in lesson four, you've got quite a long worksheet, and this one is a super long one, I just printed it, it does take quite a while, it's got a few pages, so it's important, really, really important that you read that worksheet so, so carefully.

First, read it one time through before you start actually doing anything.

And then just keep referring back to earlier parts.

You've got quite a big scenario.

You've got lots of tasks.

But the reason this worksheet is so long is because it gives you lots of support as you go along.

So syntax checklists, checklists for making sure that you're doing it right, and advice along the way.

So that's why it's so long, it's there for a reason.

So that's there to support you.

But if you do struggle, if you find it that it is just too much and you want to just code along with me, I am going to go through the solution after this.

So you can code along with me, if you really want to as well.

But it's really, really important that you do try to do it as independently, first of all, and give it a go because then you can start building up your own programming and thinking skills as you go along.

So pause the video now while you have a go at doing the pizza calculator.

Excellent! So now, what I'm going to do is I'm going to go through the solution to show you how I would have programmed the pizza calculator.

And just like with all the other things, everyone's gonnna do this slightly differently.

As long as it does what the brief asks, then it's working and it's fine.

But you might've coded slightly differently and that's fine, but this is how I would do it.

What I'm going to do then is I'm going to work through the pizza calculator task so that I can show you how I would solve the problem.

So you can follow along with me to see if you've got it right or you can just code along with me if you'd like to and you just want a bit of support.

So here we go.

So I've read the scenario and now it says "Task one, which pizza?" So it's got "create a series of print statements "on the inputs that will allow the customer "to type in their pizza requirements.

"Test your code using the example input/output below "and on the following slides." I've got some slides here with some example code.

And the example code is really useful because it gives me hints about what I might type in the print statements and what things might happen.

So I'm going to take a look at that.

So the user is prompted about their base choice.

So it says, "would you like a thin or thick crust?" So I know I'm going to have to have a print statement there, so print, "would you like a thin or.

." That's not how you spell it.

"Thin or thick crust?" Okay.

And then the next thing, the user enters a response, "thick." So I'm going to have to have a variable there.

So I could have "base" for my variable name.

I'm going to have some input and it's going to be text.

So it's fine to just leave it like that.

I'm not going to do a lot just yet.

So you've got the user-entered response, "thick," so I've put that there.

And then the user's prompted about their pizza size, so now I've got another question.

"Pick a pizza size "From eight, 10, 12, 14, or 18.

"Eight, 10, 12, 14, or 18." Okay.

And then the next thing.

Oh, inches at the end.

"Inches." The next thing it says, "somebody types in 14." So now I could have another variable called "size" and some input like that.

And then the next thing says, "would you like cheese? "Yes or no?" So another print statement, "Would you like cheese? "Y or N?" And they type in a Y or an N, so I'm going to have to have "cheese = input" so that I can get the input from the user.

And then it says, "the user is prompted "about the pizza type." So which pizza type would you like? And then you've got all the different options.

So again, print "which pizza type.

." Whoops! "Type would you like?" And then I'm going to put, on a new line, I think, different options.

Oh, I can never spell "margherita." Mar.

I'm just going to copy it.

"Margherita, "vegetable, "vegan, "Hawaiian." It's another one.

Another the difficult one to spell.

"Or meat feast." They're my pizza choices.

And then the user types in their choice.

So I can have "type equals.

." I'm going to put pizza type 'cause that's gone a different colour and sometimes, when it goes a different colour, it means that there's already a command word used for that for a bit of code, so sometimes it doesn't always work.

So that's why I put "pizza type" instead of just "type." So I've got "pizza type." And then what are they going to put? They're just going to put some string, like so.

And then it says, "If you have a voucher code, enter it now.

"Press enter to skip." So I'm going to have, "If you have a voucher code, enter it now "or press enter to skip." Okay, and then I've got to put "voucher code = input" and there we go.

And then, the programme ends at this point.

So I'm assuming that's all of task one.

Now, there is also some support of task one and it gave you some sample code and a variable with input there.

And it also had a syntax checklist as well.

So if I've made mistakes with my code there, then I could have used that to help me.

Now I'm just going to run the programme, just to check.

I haven't got any syntax errors.

So I'm going to put "thick." Spelt it wrong but it doesn't matter because I've got no check in there to see if it is right.

I'm going to choose eight.

"Would you like cheese?" Yes.

"Which pizza type would you like?" I don't know.

I dunno why I picked Margherita.

I'm going to go for vegan.

And then, "if you have a voucher code, "press enter to skip," so I'm going to skip.

And then the programme terminates at that point because that's the end of my code.

So I know I'm not getting any syntax error and it seems to be working.

So I'm ready to move on now to task two.

So task two is where we have to start really thinking, "Oh, what on earth do I do with all these values now?" Because on those first few pages, you've got different costs for the pizzas and all sorts of different scenarios and things to look at.

So you've got to really be careful what you put in here.

So it says "Task two.

"Calculate the pizza base cost.

"So make sure that a total cost variable has been created "for the total cost of the pizza." Now, that's an easy one.

So total cost.

It's so easy.

It's easy when you can spell.

So total cost, and I'm just going to put zero, 'cause at the beginning, it's going to be zero.

"Create an if statement that will apply 10 Pound "if their pizza is thin "and eight Pounds if it is thick." So what I need to do now is just check what I put as my variable.

So I put "base" for thin or thick.

So if base is equal to.

And what was the actual options? "10 Pound if their pizza is thin "and eight Pound if it is thick." So if I put "thin" there, "total cost = total cost plus.

." Let me just.

What was it per thin? Let me just double check.

So if it's thin, it's 10 Pound.

If it's thick, it's eight Pound.

"10 "Else "Total cost = total cost plus eight Pounds." Okay.

So that is my thin crust bit.

So I could, here, just put, "adding the base cost," there.

So if I put a little comment there, then that's going to remind me what those bits of code actually do, so I can keep track.

And this has put, "The user enters their pizza choice." And then adding the base cost.

And then, what's the next bit? So it says, "use a print statement "to print the total cost at the end of the code block "so that you can test that the code is working." So I'm just going to have a print there.

"Print total cost." And this is definitely good practise to do, to get used to doing so that you can test your code.

"So would you like thin or thick?" So I'm going to do thin.

"Pick a pizza size." Eight.

"Would you like cheese?" No.

"Which pizza type would you like?" Vegan.

"If you have a voucher code.

." No, press enter and it's saying 10 Pounds.

So I know it's worked for that scenario, but you should always check it as well for the other scenario too.

So I'm going to put "thick" and I'm going to put "eight." No.

Vegan.

Enter.

And it's doing eight.

So I know that they both.

Both branches are working correctly.

And again, there was some support there.

So I mean, I didn't use the support but I did actually, by by some miracle, do pretty much the same thing.

So that's fine.

And then it says, "Add the pizza slice cost." So that is task three.

So there are just two different costs for the size options.

"If the pizza is larger than 10 inches, "then additional charge of two Pound is applied.

"Create an if statement that will apply this charge based "on this condition." Okay.

So now I'm going to put "adding the size cost" there.

So I know what I'm doing there, at that point.

So if the size of the pizza is greater than 10.

I did not call it size, let me just double check.

Yes I did.

So if size is greater than 10.

Oops.

Then what do I want to do? So if the pizza is larger than 10 inches, then an additional charge of two Pound is applied.

And it just says create an if statement 'cause it's only going to need one branch.

I don't want to print so I've got to change the total cost.

So total cost equals total cost plus two Pounds.

Now, I'm going to print it and I'm just going to show you an error.

'Cause I know an error is going to happen and I want you to see what the error might be.

You might've already figured it out.

Let's just have a look.

So I'm going to put "thin." I want it to be more than 10, so I'm going to put 12.

"Would you like cheese?" No.

Vegan.

Spelt it wrong.

"If you have a voucher code.

." No.

Okay.

So we've got an error message there.

Now, why do you think we got an error message at that point? So if we look here, it's got "type error '>' not supported "between instances of string and integer." So let's just take a look at this.

It's going greater than 10 and that is an integer but if I look here, size, if I go all the way up to here to size, at the moment, it's string.

So what do I have to do to this input to make sure that I'm using an integer value? I've got to do an int, haven't I? So "int" and then wrap it around.

There we go.

And now, hopefully it should work.

So thin, 12, yes, vegan, voucher code.

Okay, so now it's printing in both times.

I think prints yesterday.

So I've got 10 and then he's adding that additional one, which is 12.

And in fact, I just need to get rid of that print.

I'm going to get rid of that print as well.

Okay, so that's adding the size cost.

So then we've got Task Four, "Cheese or No Cheese." So if the cheese is not equal to yes, then a discount of 50% is applied to the total.

Now we want it to be a Y, a lowercase Y as well ao we've got a.

whoops, I'm going to just put a comment there.

Let's just scroll it up.

There we go.

Cheese or no cheese.

So then we've got, "if cheese is not equal to a lowercase Y, "then they get a discount of 50 pence." Is that right? Yes, they do.

So then we've got total cost equals total cost minus zero 0.

50, 'cause that's my 50 Pence.

And again, I'm going to check that by putting a print.

Print total cost.

Whoops.

Okay.

And let me just check.

So this is not equal to yes.

So that would mean they put no.

Okay.

Right.

So now, I need to put "no" for cheese and just see if it gets 50 pence knocked off.

So thick, 12, no, vegan, enter key and it's nine Pound 50.

So because it's got a 50 pence there, SI know that it's deducted 50 pence.

Okay, so I am getting there.

All right, so then we've got pizza types.

So here we go.

There's quite a lot of instructions for this one.

"There are three different pricing options for the pizza.

"Margherita doesn't have an additional charge." Okay, so we can ignore that one.

"So decide if this needs to be part of your conditions." No, it doesn't.

"If the pizza is vegetable or vegan, "then there is an additional charge of a Pound.

"If it's Hawaiian or meat, "then there was an additional charge of two Pounds." So I might need an ELFS.

No, I think I just need an "if" and an LF actually, now I think about it.

So I can try it anyway, I can test it and see if it works.

So I'm going to get rid of that one.

In fact, no, I'm just going to leave it there.

Just eave it down at the bottom.

So then, now we've got types.

So adding the pizza type cost.

So now, I've got to think, "well, what on earth do I put next?" So we've already got if statements so I can use some of those if statements.

And it's going to be an elif.

So if.

What was it? Type? Go all the way up.

So I used pizza type this time.

So if pizza type.

And I'm going to do it all lower case.

If pizza type is equal to.

So what were the options? So if it's vegetable or the vegan, then I charged a Pound.

So if pizza type is equal to vegetable or pizza type equal to vegan, then what was it? So it's vegetable or vegan.

There is an additional charge of a Pound.

So then I do total cost equals total cost plus a Pound.

And then I've got an Elif, pizza type is equal to.

Which ones are the other ones? So Hawaiian or meat feast.

So make sure we do it lower case.

Hawa.

Never spell it right.

Hawaiian.

Is that right? Ha-w-aii-an, I think it's right.

or pizza type equal to meat feast.

Then the total cost is going to be added by a Pound so I'm just going to copy that, so I don't have to keep typing it out.

And it was two Pound for that one.

And there was no additional charge for the Margherita, so we don't actually need a condition there.

But let's just check it and just see what happens.

So, "would you like thin or thick?" So I'm going to go for thin pizza.

It's going to be 18 inches.

"Would you like cheese?" We're going to have, no.

And, "which pizza type would you like?" Margherita vegan.

Which one do I need to test? I'm going to test margarita first.

So Margherita.

"If you have a voucher code.

." I haven't done that yet.

So 11 Pounds 50.

So it hasn't made any changes.

I went for the larger pizza, so that's why it's 11.

50.

I didn't have cheese either.

Okay, so that's working.

But let's see what happens if I type in "meat feast" this time.

So I'm going to have 18 again.

No cheese.

And I'm going to have Meat Feast.

11 Pound 50.

Is that working? I need to do the additions in my head now to see if it's working.

So I went the thick crust, which is eight Pounds.

And I went for 18 inches, which is two Pounds.

That's 10 Pounds.

I didn't have cheese.

So that would be nine Pound 50.

And then I went for meat feast, which adds two Pounds.

It's nine Pound 50, 10 Pound 50, 11 Pound 50.

Yeah, so it's working.

So sometimes you do have to do the maths in your head just to double check that you've definitely got it right just like I did there.

So I'm pretty sure now that my code is working for the pizza types.

And then you've got there, the voucher code.

So it says "the voucher code can be applied "when the customer purchases an 18 inch pizza "and has typed in the correct code, "which is "Fun Friday." "Create an if statement that checks "that both conditions are true "and then applies a two Pound discount." So I'm going to have another one called voucher code.

So if voucher code is equal to Fun Friday.

And then what was the other one? And.

So I've definitely done that right.

Let me just have a look.

So voucher code, I've definitely spelled that right.

And then it was.

Was it pizza type is.

No.

Pizza size.

So I've just got size there.

Size is equal to 18.

So, "and size is equal to 18." Now, that's not string.

It's not string, is it? So I've got to put 18 like that, okay? Then it's going to do this bit of code again.

So I'm just going to copy and paste it.

But what's the value? They get a two Pound discount this time.

So I can take that off.

So I'm going to run the code again.

"So would you like thin or thick?" I'm going to go for thin now that it has to be 18 for me to test it.

"Would you like cheese?" No.

Did it have to be a pizza type? No, it didn't.

Voucher code is "Fun Friday." 10 Pound 50, o I'm pretty sure that's worked because it's cheaper than it was before.

Now, it does tell you to test your code really, really well, which I've got there.

And that says, "Test your code "by entering all the different possible scenarios "for ordering a pizza." Now I've done that quite a lot as I've got along, So I'm not going to spend too long testing it with you.

But if you were doing this by yourself, I would certainly want to see you testing as many scenarios as you can.

And it also suggests that you start using dot upper or dot lower as well on those inputs, so I think that's a good idea too.

Just remember to not put it on the int one because it's not needed.

Okay.

So now, my code is pretty much spot on.

The final task then is to see if you can spot five errors in this programme.

So pause your video while you see if you can spot those five errors.

Great, let's look at the answers then.

So here's your first one.

So it says "should be a single equal sign here, "not a double equal sign." You've also got there, an "and" in uppercase when it should be lowercase.

You've got a single equal sign there when it should be a double at this point because this is saying equal to.

You've got capital E for else.

That said, it's never a good thing, is it? And you've also got a missing bracket there at the end.

Fantastic, wow! You have done extremely well to get all the way through to the end of this lesson.

I'm really, really proud of your.

Well done.

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

I'd always love to see what you've been getting up to.

And I'll see you again soon for the final lesson in this part of the unit.