top of page
top

Python 4c - Logical Operators

noun-decision-3428301-FFFFFF.png

AND Operator

The AND operator is used to execute certain code if more than one thing is true.

​

AND is commonly used with account logins - both the username AND the password must be correct.

​

The example below requires both a secret word and a secret number to be correct:

print("To enter you need the secret word and the secret number!")
word =
input("What is the secret word? ")
number =
int(input("What is the secret number? "))

 

if word == "solitude" and number == 2011:
   
print("Correct! You may enter!")
else:
   
print("Incorrect! Get out of here!")

If no part or only some of the if statement is true then the indented code will not run:

To enter you need the secret word and the secret number!
What is the secret word?
solitude
What is the secret number? 4503
Incorrect! Get out of here!

To enter you need the secret word and the secret number!
What is the secret word?
windhelm
What is the secret number? 1021
Incorrect! Get out of here!

Only If all parts of the if statement are true will the indented code be executed:

To enter you need the secret word and the secret number!
What is the secret word?
solitude
What is the secret number? 2011
Correct! You may enter!

Logical Operators Task 1 (Three Easy Questions)

Ask the user three easy questions and print a special response if they get all three correct.

​

Use the and operator to see if their answer for all each of the questions is correct.

​

You must use a unique variable name for each of your inputs (it can't be 'answer' for all three, for example).

Example solutions:

What is the capital of Germany? Berlin
What is the chemical formula for water? H20
What year did World War Two end? 1945
You absolute genius!

What is the capital of Germany? Vienna
What is the chemical formula for water? W20
What year did World War Two end? 1945
Bit awkward, I thought you'd do better...

OR Operator

The OR operator is used to execute certain code if one of several statements is true.

 

The program below is checking if either a, e, i, o or u were entered.

letter = input("Enter a letter: ")

​

if letter == "a" or letter == "e" or letter == "i" or letter == "o" or letter == "u":
 
print("You entered a vowel.")
else:
 
print("You entered a consonant.")

Enter a letter: f
You entered a consonant.

Enter a letter: e
You entered a vowel.

It is important that you re-write the variable and operator (e.g. letter ==) each time you use 'or'.

 

It will not work if you just write: if letter == “a” or “e” or “i” or “o” or “u”:

Logical Operators Task 2 (Twins?)

Ask the user to enter their favourite colour and then ask them their age.

 

If their favourite colour is the same as yours AND their age is the same as yours then print “Snap! Are you my twin?”.

 

If only one of the statements is true (use the OR operator) then print “Spooky! You’re a bit like me.”.

​

Add an else statement to print “We’re not so similar, you and I.” if there's nothing in common.

Example solutions:

What's your favourite colour? green
What's your age? 15
Snap! Are you my twin?

What's your favourite colour? blue
What's your age? 15
Spooky! You're a bit like me.

What's your favourite colour? red
What's your age? 16
We're not so similar, you and I.

bottom of page