CSwithZain0 / 28passed
Block B · steps 8 to 16

Decisions

0 of 9 passed
The idea

An if asks a question that is either True or False, and runs one path or the other.

One sign carries most of this block. One equals puts a value in. Two equals asks a question. Mark = 70 stores 70. Mark == 70 asks whether Mark is 70, and gives True or False.

When there are several tests in a row, Python checks them top to bottom and stops at the first one that is True. A cinema sells three tickets: Child under 12, Adult under 60, and Senior for everyone else. Pick an age and step through it.

Step through itstart
Age
·
Test
·
Pick an age, then press Step.
Output

Try 12. It is not under 12, so the first test is False and the Adult test catches it.

Reference

Comparing, and the signs that change from pseudocode

PseudocodePythonMeans
===is equal to
<>!=is not equal to
<   >   <=   >=<   >   <=   >=the same in both
AND   OR   NOTand   or   notlower case
MOD%remainder
The shape

if, elif, else, and the indentation is the ENDIF

Every if line ends with a colon, and the lines it controls are indented underneath. There is no ENDIF: the indentation shows where the branch stops.

one path out of three

elif means "else, if". Use as many as you need. else has no test: it catches everything the tests above did not.

CASE OF in pseudocode becomes an elif chain in Python.

Order matters

The first True test wins, so put the narrowest first

Both programs below use the same three tests. Only the order changed.

wrong order
right order

5 is under 60, so the wrong order stops at the first test and never asks about 12. A child pays the adult price. Grade bands work the same way: test Mark >= 80 before Mark >= 70.

Text

Comparing text: capitals, and numbers that are still text

"yes" == "Yes" is False. Capital letters count. Convert first, then compare with one spelling:

any capitals work

Text compares letter by letter, not by value. As text, "9" > "10" is True, because the character 9 comes after the character 1. Forget int() on two ages and your program names the wrong person as older, with no error at all.

text against numbers
Swapping

To swap two boxes you need a third

Putting three numbers in order means swapping two of them when they are the wrong way round. A = B then B = A does not work: the first line wipes out the old A. Keep it somewhere first:

a swap

You will use exactly this in bubble sort, much later in the course.

Watch out

Six ways this block goes wrong

  • TRAPUsing one equals in a test.

    if Mark = 70: stops with a SyntaxError. A test needs ==.

  • Tests in the wrong order.

    The wider test catches everything first. Narrowest test at the top.

  • Comparing numbers that are still text.

    "9" > "10" is True. Convert with int() as you read.

  • Forgetting the colon.

    Every if, elif and else line ends with :.

  • Capitals in text answers.

    Use .upper() on what was typed, then compare with capitals.

  • Writing AND or <>.

    Python wants and and !=.

Scope

What you do and don’t need

You do need

  • if, elif and else, indented correctly
  • all six comparisons, plus the words and, or and not
  • % for remainders, // for whole division
  • .upper() before comparing text
  • swapping two values with a spare variable

Not yet, or not at all

  • loops: block C
  • Python's match statement. An elif chain does the same job and is what this course uses
  • in for checking several values at once: this course writes each test with or so you see every one
Exam thinking

Test the edges, not just one easy value

A grade program that works for 72 can still be wrong at 70. Run it with a value from every band and with the exact boundary values: 69, 70, 79, 80. The mistakes in decisions hide at the boundaries, usually a > that should have been >=.

Remember these three

  1. One equals stores. Two equals asks.
  2. Python checks top to bottom and stops at the first True test. Narrowest test first.
  3. Convert numbers before comparing them. As text, "9" > "10".
Now practise

Steps 8 to 16

Step 14, Each digit as a word, uses the operators from Block A. Read it there.

See all 9 steps

Block B · DecisionsProgress is saved on this device.

Each step is checked against its exact output. Open one to read the task, see a sample run, and write your program. If you get stuck, the task opens the notes that teach it.

  1. 8Even or oddAsk for a whole number. Print Even or Odd.Open
  2. 9Positive, negative or zeroAsk for a number. Print Positive, Negative or Zero.Open
  3. 10Who is olderAsk for two people's names and ages. Print who is older. The two ages will never be the same.Open
  4. 11The right collegeAsk which college. If it is MIT, print Lets go (with no apostrophe). Otherwise print BOO. It must work however the user types it: MIT, mit or Mit.Open
  5. 12Marks to a gradeAsk for a mark out of 100. Print Grade A for 80 or more, Grade B for 70 to 79, Grade C for 60 to 69, Grade D for 50 to 59, and Grade U for anything below 50.Open
  6. 13Leap yearAsk for a year. Print Leap year or Not a leap year. A year is a leap year if it divides exactly by 4, but not if it divides by 100, unless it also divides by 400. So 2024 and 2000 are leap years, and 1900 and 2023 are not.Open
  7. 14Each digit as a wordAsk for a three-digit number. Print each digit as a word, with a space between them, and 0 as zero: 472 gives four seven two. Hint: 472 // 100 is 4, 472 // 10 % 10 is 7, and 472 % 10 is 2. This one is long on purpose: each digit needs its own if and elif chain.Open
  8. 15Cheapest of three StretchAsk for three prices. Print the smallest.Open
  9. 16Three inputs in ascending order StretchAsk for three whole numbers. Print them smallest first, on one line, with one space between them.Open

On a phone? Read the tasks and plan on paper here. The coding is easier on a laptop, where the same link opens the same step.

When you want more than practice

The steps are the front door. The class is where it clicks.

Join a class →Ask on WhatsApp