7_3_3

WHAT: Describe and use Boolean logic in selection.

HOW:

Activity 1

You were introduced to Boolean logic in outcome 7_2_9. If you need reminding of this then re-visit the outcome.

Activity 2 - READ

Boolean logic is when a condition can either have a True or a False outcome and nothing else.

If I said the condition "It is Monday". This could either be True or False.

If I said the condition "My password is Cheese". This could either be True or False.

If I said the condition "The light is on". This could either be True or False.

If I said the condition "My age is a higher number than yours". This could either be True or False.

We can use these conditions in programming to make decisions.

Boolean logic operators that you should be familiar with are AND, OR and NOT. You use these words everyday when you talk to other people.

e.g. If it rains tomorrow I am NOT going for a bike ride.

Boolean is also a "data type", just like string and integer. A Boolean data type can only store two values. In most cases this is True or False.

Activity 3

Try this bit of code in Python:

password = ("cheese")
passTry = input("Type the password")
passwordCorrect = (password == passTry)
print (passwordCorrect)

Run the code twice, once getting the password right and another time where you get the password wrong.

What changes with the final print?

Hopefully you noticed that it displayed True when you got it right and False when you got it wrong.

Activity 4 - just going off topic for a second

Just out of interest, try this little bit of code at the bottom as well:

print (type(passwordCorrect))

When you run this bit of code the output looks like the screenshot below:

This last line of code is showing you the data type being used with the variable "passwordCorrect". You could take a minute to try some other data types to see what it says!

Activity 5 - back to the outcome!

So this is the code that you should have right now:

password = ("cheese")
passTry = input("Type the password")
passwordCorrect = (password == passTry)
print (passwordCorrect)
print (type(passwordCorrect))

We are going to change this a little bit now so that it becomes an actual password checker.

Make the changes shown below to your code (or just copy and paste it if you need to):

password = ("cheese")
passTry = input("Type the password")
passwordCorrect = (password == passTry)
if passwordCorrect:
    print ("Access Granted")
else:
    print ("Access Denied")

Run the code and see what happens when you get the password right and when you get it wrong.

Keep looking at the code until you REALLY understand it. Here is my explanation:

The password for entry is cheese. This is stored in the variable "password". The user attempts to type a password and this is stored in the variable "passTry". For the password to be correct then "password" must be equal to "passTry", we store this in a Boolean variable called "passwordCorrect". Next, we have a selection statement. This says that if the password is correct then state "Access Granted" otherwise state "Access Denied".

Activity 6

We are now going to use the word AND in our Boolean logic statements. Try this piece of code:

username = ("android101")
password = ("greenrobot")
usernameTry = input("Username : ")
passwordTry = input("Password : ")
entryAllowed = ((username == usernameTry) and (password == passwordTry))
if entryAllowed:
    print ("Welcome, you are in!")
else:
    print ("Access unauthorised")

Try all of these scenarios with the code to see what happens:

  1. Get the username and password right
  2. Get the username right and the password wrong
  3. Get the username wrong and the password right
  4. Get both the username and the password wrong

It should have only displayed the text "Welcome, you are in" when you tried scenario 1. This is because the username AND the password must be right in order for that Boolean statement to be True.

Activity 7

We are now going to use the word OR in our Boolean logic statements. Try this piece of code:

age = int(input("Age :"))
correctAge = ((age == 10) or (age == 11))
if correctAge:
    print ("You are old enough to play this game")
else:
    print ("Sorry, you can't play this game")

Try all of these scenarios with the code to see what happens:

  1. Type 10
  2. Type 11
  3. Type 4
  4. Type some text (string)

Only scenario 1 and 2 should have shown the output "You are old enough to play this game". 3 would deny access and 4 would cause an error message.

Before moving on, use your knowledge from the last outcome to stop that error from scenario 4 happening. Re-visit 7_3_2 for the code if you need to.

CHECK:

EMBED:

Option One

Try and use selection in Scratch. This is a little different to Python so you may want to find an online tutorial to help you.

Option Two

Create one of the following mini programs:

  • Get the user to guess two secret words to reveal a secret message
  • Make a multiple choice quiz with at least 4 questions
  • Make a guess the word game.

CLASSROOM IDEAS:

To refresh the students on AND, OR, NOT it is helpful to re-visit the classroom activity from 7_2_9 as a warm up to this lesson.