replit

Before you start this course, you must login to Replit

If you have forgotten your Replit account user name or password, ask your Coder Coach.

CodeSports

9.1 Boolean Predict and Run

Students will read the example code in main and predict what will happen if the code runs

Instructions

Read the example code in main and write predictions on what will happen if the code runs. Make sure to write your predictions in comments (#). For more clarification, refer to the instructions file under the lesson folder

Advanced Selection

This topic covers three more complex skills associated with selection:

  • Boolean operators for more complex conditions (more than one condition used to trigger a selection branch)
  • Validation of numbers (check whether a number is inside or outside a range – super useful)
  • Nesting (putting one selection statement inside another)

Learning Objectives/Goals

Be able to read, comprehend, trace, adapt and create Python code using selection that:

  • Uses Boolean operators with multiple conditions.
  • Checks if a number is inside or outside a range.
  • Nests selection statements inside each other.

Slide Deck

The slides to accompany this topic are here

TEACHER NOTES

In the ‘Intro To’ course we used selection with a single condition to decide whether to execute the if, elif or else instructions.

Here we use **Boolean operators **to allow us to create conditions that check more than one true or false condition.

We are using this technique with selection, but it can be used anywhere you would use conditions, for example in conditional (while) loops.

Make sure that pupils are aware that each condition must include the data or variable being checked. Lots write code like this: num1 < 5 and < 10

The operators are:

and – checks more than one condition, only returns true if ALL conditions are true.

num_1 = 9
#returns false, because only one condition (num_1 < 10) is true.
num_1 < 5 and num_1 < 10

#returns true, because both conditions are true.
num_1 < 20 and num_1 < 10

#returns false, because only two conditions (num1 < 10, num1 < 20 are true)
num_1 < 5 and num_1 < 10 and num_1 < 20

or – checks more than one condition, returns true if **AT LEAST ONE **condition is true. Another way of thinking about this is that or only returns false when all conditions are false

num_1 = 9
#returns true, because one condition (num_1 < 10) is true.
num_1 < 5 and num_1 < 10

#returns true, because both conditions are true.
num_1 < 20 and num_1 < 10

#returns false, because all conditions are false
num_1 < 5 and num_1 < 9 and num_1 < 7

not – returns the opposite of a condition – if the condition is true it returns false, if it’s false it returns true

num_1 = 9

#returns false because the condition is true.
not(num_1 < 10)

#returns true because the condition is false.
not(num_1 < 5)

#returns true because the condition is false.
not(num_1 != 9)

Numbers in a Range

TEACHER NOTES

Validation is the process of checking whether data is sensible and allowable. It’s a really important part of lots of programs that involve input (for example checking your password).

We can use Boolean operators to check whether a number input is within (or indeed outside) a range.

Note – we are using selection to output a relevant message. This doesn’t make the program loop to ask for the input again – we will cover that when we look at iteration in the next set of tasks.

To check if a number is within a range, we use:

Greater than or equal to the lowest value AND less than or equal to the highest value.

Both conditions have to be true to ensure that the number is within the range.

CODE EXAMPLES

Checks if the user input is between 1 & 10 and outputs a relevant message

num_1 = input("Enter a number between 1 and 10")

if num_1 >= 1 and num_1 <= 10:
	print("Number in range")
else:
	print("Number not in range")

Same as above, but using < & > instead of <= & >=

num_1 = input("Enter a number between 1 and 10")

if num_1 > 0 and num_1 < 11:
	print("Number in range")
else:
	print("Number not in range")

To check if a number is outside a range, we use:

Less than the lowest value **OR **greater than the highest value.

Only one condition has to be true to ensure that the number is outside the range. It would be impossible for both conditions to be true

Checks if the user input is not between 1 & 10 and outputs a relevant message

num_1 = input("Enter a number between 1 and 10")

if num_1 < 1 or num_1 > 10:
	print("Number not in range")
else:
	print("Number in range")

Same as above, but using <= & >= instead of < & >

num_1 = input("Enter a number between 1 and 10")

if num_1 <= 0 and num_1 >= 11:
	print("Number not in range")
else:
	print("Number in range")

Nesting

TEACHER NOTES

Nesting is the term for putting programming constructs of the same type inside each other.

These examples use nested selection – if statements inside other if statements.

Replit makes it easier to track nesting by putting vertical lines in the code that run between the parts of the same statements.

Students can easily get confused between the statements, get them to trace vertically to help them understand the flow.

The nested statement will only be run if the condition above it is true.

CODE EXAMPLES

if num_1 > 20:
  print("Bigger than 20")

# nested if is indented so is part of the 'if' statement above.
  if num_1 > 50:
    print(" and bigger than 50")
  else:
    print("but not bigger than 50")

Help! My Code Does Not Work!


Make sure to check your code for the following things:

Help! My Code Does Not Work!

Make sure that you check for the following things:

  • there is a full condition (two pieces of data and a comparison operator) on **each side ** of the Boolean operator. Example:
x < 5 and  x < 10
  • the Boolean operator is in lower case
  • the correct Boolean operator for the result you want has been used (check the logic)

Hints