Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

In an investigate task you take working code apart to see how each piece contributes to the result. Answer every question for yourself first. You can edit the code and run it to check. Only then open the answer.

Run the program a few times before you start: once with 4 and 9, once with 9 and 4, and once with 5 and 5. Between them those three runs use every branch in the code.

print("Please enter a number")
number1 = int(input())
print("Please enter another number")
number2 = int(input())

if number1 > number2:
    print("Number 1 is bigger than number 2")
elif number2 > number1:
    print("Number 2 is bigger than number 1")
else:
    print("Both numbers are the same")

Where does the selection start in this program?

Answer

At the line if number1 > number2:.

Everything above that line collects data: the two input calls ask the user for a number each and store what comes back. Nothing has been decided yet. The if is the first line that looks at the data and chooses what to do with it, and the selection runs from there down to the end of the else branch.

How many selection statements are there in the code?

Answer

One.

It is tempting to count three, because there are three keywords and three print lines. But elif and else cannot stand on their own: they only mean anything as part of the if above them. The whole if / elif / else block is a single selection statement that picks one of three branches, not three separate decisions.

How many conditions are there in the code?

Answer

Two: number1 > number2 and number2 > number1.

The if has one and the elif has one. The else has none, and that is exactly what makes it the catch-all: there is nothing left for it to test, because it only ever runs when both of the conditions above it turned out to be false.

Three outcomes, two conditions. That is the general pattern: a chain needs one condition fewer than it has outcomes, because the else mops up whatever is left.

What does the > operator mean?

Answer

Greater than, or “is bigger than”. number1 > number2 compares the two numbers and produces True when the one on the left is bigger, and False otherwise.

It is a comparison operator, so it does not calculate anything and it does not change either variable. All it produces is the True or False that the if needs in order to decide whether to run its branch.

> is strict: 5 > 5 is False, because 5 is not bigger than 5. Two equal numbers fall through both conditions here and land in the else.

Why are the three print lines indented?

Answer

Because the indentation is what puts each print inside a branch.

Python uses indentation to work out which lines belong to which part of the selection. print("Number 1 is bigger than number 2") is indented under the if, so it runs only when that condition is true. The other two are indented under the elif and the else, so each of them runs only when its own branch is chosen.

Take the indentation away and the lines no longer belong to any branch, so all three would run every time, whatever the user typed. The if, elif and else lines themselves stay at the left hand margin, because they are the selection rather than part of a branch.

What is the purpose of this program?

Answer

It asks the user for two numbers and tells them which of the two is bigger, or that the two are the same.

There are exactly three things that can be true of two numbers, and the program has one branch for each of them, so every possible pair of inputs produces exactly one result line, after the two questions.

Help! My code doesn't work!

Make sure that you check for the following things:

  • Selection begins with if, has as many elif branches in the middle as you need, and ends with else.
  • There is a colon at the end of each if, elif and else line.
  • Every line inside a branch is indented, and all of them by the same amount. Never mix tabs and spaces in one file.
  • The if, elif and else lines themselves are not indented.
  • There is a condition after each if and each elif.
  • There is no condition after else.
  • You have used a double equals (==) in a condition that checks whether two pieces of data are the same. A single = stores data in a variable.