video

Lesson video

In progress...

Loading...

Hello, I'm Mr. Hogan.

We're going to be learning about how to programme using Python this lesson.

I'm so happy that you are able to join me for this amazing lesson coming up.

Shall we make a start? This is the sixth lesson from the, "Introduction to Python Programming Unit." It is called, "Building a Programme Using Control Structures." The Outcome to this lesson is I can combine sequence, selection and iteration to build a programme that uses complex conditions.

You might find it difficult along the way or a bit challenging, but please, you can pause the video at any time to have a think about what I've just said or you can rewind it and start at a point that you didn't understand.

But hopefully, you'll find it really interesting and get to the end of the lesson with success.

There are three Keywords in today's lesson.

Condition, this is an expression that evaluates to True or False.

Selection, so this is used when there is more than one possible path for a programme to follow.

And we have iteration.

This is the process of repeating a sequence of instructions within a programme loop.

There are two parts to today's lesson.

The first part is build complex conditions and the second part is combine programming structures in a programme.

Let's have a look at the first part of the lesson, which is build complex conditions.

Logical operators are used when you need to compare multiple values with relational operators to return a single value of True or False.

The three logical operators are and, or, not.

Let's have a look at the descriptions of these operators.

So the and operator, it's True only if both conditions are True.

Or, is True if at least one condition is True.

Not, this reverses the answer.

So True becomes False and False becomes True.

So Lucas is saying here, "I use logical operators in my own decision making.

If it's raining and I have an umbrella, then I will go outside." Let's have a quick check.

To log into a system, the username needs to be admin and the password needs to be password123.

Which of the following login attempts would return True? A, username, admin, password, wrongpassword.

Is it B, username, user123, password, password123.

Or C, username, admin, password, password123.

Have a think and think about it.

You can pause the video at any time.

The answer is C, username, admin, and password123.

I hope you got that right.

That's the first check of the lesson, well done.

Let's have a look at this code.

This is the Lucky Number Programme and it's been extended to only allow the user a maximum guess of three guesses.

We do this by using and, and a variable which we've called count, and this will limit the number of guesses.

So we can see on line five, we've added, and count less than three.

So both conditions need to be True, in order for the code below it on line six to line 14 to run.

So let's have a look at this in a bit more detail.

So this line, while guessed equal False and count less than three, this defines the loop's condition.

The loop continues as long as both of the conditions are True.

Guessed equals False.

The number hasn't been guessed correctly.

Count less than three, the number of guesses is less than three.

And putting 'em all together, the and operator combined the two conditions, so both conditions need to be True to return True.

Let's have a practise.

You are deciding if you should go on a bike ride based on the weather.

Here are some weather conditions, is_raining, so True and that's when it's raining, is_windy, so True if it's windy, temperature, the temperature in degrees Celsius.

So now write a conditional statement for the following.

A, go for a bike ride only if it's not raining.

B, go for a bike ride only if it's not raining and the temperature is at least 15 degrees Celsius.

C, go for a bike ride if it's not raining, the temperature is at least 15 degrees and it's not windy.

Have a little think about it.

Write down the conditional statements anywhere.

You can always pause the video anytime as well.

Two, what will the code print if the values of the variables are A, is_sunny = False, is_raining = True.

B, is_sunny = False, is_raining = False.

So have a look at the code, have a think and see if you can work that A and B out.

Remember you can pause the video at any time or even rewind, even go back and rewind it and look at some parts that maybe will help you understand how to answer this practise question.

Let's have a look at the answers.

So we asked you to write the conditional statements for the following.

So A, go for a bike ride only if it's not raining.

So we can just write, not is_raining.

B, go for a bike ride only if it's not raining and the temperature is at least 15 degrees Celsius.

So here we can use the and operator.

So now it becomes not is_raining and temperature is greater than or equals to 15.

C, we asked you to work out this one.

Go for a bike ride if it's not raining, the temperature is at least 15 degrees and it's not windy.

So we've got the repeat of the answer from B, not is_raining and temperatures greater than or equals to 15 and we've added an, and not is_windy.

Two, what will the code print if the values of the variables are the following? So let's look at A, is_sunny = False, is_raining = True.

A, the answer is, maybe we should stay inside today.

B, is_sunny = False, is_raining = False.

So the answer is, maybe we should stay inside today.

Excellent, we're halfway through the lesson.

Well done for keeping up with all that.

We're gonna move on now to combining programming structures in a programme.

The main programming control structures are sequence, selection and iteration.

So sequence here is represented with a flow chart with a number of symbols running one after another.

Selection is represented in a flow chart by a decision symbol, which you've got a True or False coming off it.

And then iteration uses the selection and then runs a block of code if this condition is True and loops around until that condition becomes False.

So sequence is the order of commands in a programme.

Instructions are run in the order they appear in the code, one after another.

Selection is used when there is more than one path of a programme to follow.

Code is run based on whether a condition is True or False.

An if statement is an example of selection.

Iteration, also known as looping, allows you to run the same lines of code more than once.

A while loop is an example of iteration.

Let's have a quick check.

Which programming structure is used in this code snippet? So take a look at the code.

Is it A, calculation, B, iteration, or C, selection? Take your time.

Remember you can pause the video at any point.

The answer is C, selection, because there's multiple paths for the programme to follow.

Okay, we're gonna move on to a scenario around an ATM machine.

I hope that you're following along and you're doing really, really well to keep up with it.

Remember you can pause the video at any point or rewind it and start the video from a point that you think you may need to go back to.

So a programme has been created that stimulates a basic ATM machine, a cash-machine.

The programme starts by welcoming the user.

Then it asks the user to enter the PIN 1234.

The programme checks the PIN.

If it's correct, the message, "Access Granted," is output.

If it's wrong, the message, "Access Denied," is output.

The programme uses if and else to check if the PIN is correct.

So have a look at this code.

We can see on line four, the first condition, an if statement.

So if pin is equals to 1234.

If it's True print, "Access Granted." If it's False, go to line six, and else, print, "Access Denied." The ATM programme has been extended now.

It now allows the user to try to enter their PIN up to three times.

After three incorrect attempts, the message, "Account Locked.

Please contact customer service," is output.

If the correct PIN is entered, the message, "Access Granted," is output and the programme is ended.

Okay, so take your time to just quickly read the code or take your time to read the code or pause the video and then read the code.

Okay, let's have a look at this in a bit more detail.

So straight away on line four, a flag is set to initial value of False.

So access is set to False.

The flag access on line 13 is changed to True when the user enters the PIN correctly.

On line six, this condition checks if the user has less than three attempts and the flag is set to False.

And on line 17 and 18, if the user enters the PIN incorrectly three times, the, "Account Lock," message is output.

Let's have a quick check.

Which of the following statements is true about the ATM programme? A, the user has unlimited attempts to enter their PIN.

B, if the user enters the correct PIN, the programme will display, "Access Granted," and immediately end.

Or is it C, the while loop will continue running even after the user enters the correct PIN.

Have a think about it.

You can pause the video at any time.

The answer is B, if the user enters the correct PIN, the programme will display, "Access Granted," and immediately end.

Well done, if you got that right.

And if you didn't, hopefully you can understand why it's B and carry on with the lesson.

We're gonna do a practise now.

One, design a simple vending machine programme.

The programme should do the following things.

Display three items with their prices.

So display a chocolate bar with 10 coins, crisps cost 15 coins, and a drink cost 20 coins.

Then ask the user to enter their choice A, B, or C.

If the choice is valid, output the price of the chosen item.

If the choice is invalid, output an error message.

So have a think about how you're going to do this.

Perhaps you want to write some notes down about what you're going to do, what programming structures you're going to use in this simple vending machine programme.

Take your time, have a think, and you can always pause the video.

Let's move on to two.

So extend the vending machine programme to allow the user to enter more than one choice of item.

A little hint here, you'll need to use iteration to continue to ask the user if they want to buy more, until they say they are finished.

And then three, add to the new items to a total_price variable and display this to the user at the end of the programme.

So you may want to add to your notes that you've made and see how you're going to solve these two questions in your programme.

How are you gonna combine programming structures? What are you going to use? Have a think.

Remember you can pause the video at any time.

Okay, let's have a look at at a suggested answer for one.

Design a simple vending machine programme.

So the programme should display the three items with their prices.

So we can see we have three variables on the lines one to three, chocolate_price, crisps_price and drink_price.

And there are assigned values, which are their amount in coins.

And then we've got simple print functions on five to eight, which displays their relevant price.

And then on 10 and 11, we've got print, Enter your choice, and then choice equals input.

If the choice is valid, we've asked you to print the price of the chosen item.

If the choice is invalid, we've asked you to write to the programme so it prints an error message.

So continuing the programme onto line 12, we've got some if statements and elifs and else.

So we're saying the programme is programed to look at choice and see if it equals to A on line 12.

And if that's True on line 13 runs.

And then we've got similar going down the programme, where we ask if choice is equal to B, if choice is equal to C or do the else, which is the error message, and the line 19, we've got print, Invalid Choice.

Hopefully you've got something similar to this.

If not, there's a link here to the solution for each of you to look at and experiment with and test and also use it for the next part if you want to.

So two, we've created a flag more_items, and set it to an initial value of True on line four.

And on line 11, we've got a while loop with the condition, more_items equals True.

We've also got an if statement to change more_items flag if the user doesn't want more items. So take a look at this code, which we've zoomed in on.

So we've got the question, "Do you want more items," Y and N.

So therefore, the user will input Y or N, and that becomes assigned to the more variable.

And then the if statement has a condition if more equals Y.

So if that is True, more_items flag becomes True.

If it's False, the more_items flag becomes False.

Three, we've put a total_price variable in on line five and we set the initial value to zero.

The value of the variable total_price is updated on line 17, 20 and 23, if the user selects an item.

So the total_price variable is equals to the total_price plus the chocolate price for example.

The value of total_price is output to the user at the end of the programme.

Well done, you've come to the end of this lesson.

It was a bit tricky in parts and a bit challenging, but hopefully you've paused the video and you've looked at parts of the video again, perhaps that you found a bit challenging, but you've made it to the end, which is fantastic.

So in summary, hopefully you found that logical operators and, or, and not, can be used in conditions to help programmes make complex decisions.

Sequence selection, and iteration are control structures that can be combined to create complex programmes like the vendor machine programme.

Well done again, for completing this lesson.

Hopefully you found the lesson really good, and you've learned lots of stuff about Python programming.