Conditions in Python
Chapter 6: Conditions
6 short lessons, with 3D animations, quizzes and coding tasks.
The lessons
Making decisions with if
if runs its indented block only when its condition is True, and either way the program carries on below.
else and elif
Python checks the if, then each elif, from top to bottom, runs only the first block whose condition is True, and else catches everything left over.
Combining conditions: and, or, not
and, or and not combine True/False answers into one answer.
Nested ifs and one-line choices
An if can sit inside another if's block, and a if condition else b picks one of two values in a single line.
match and case
match compares one value with each case from the top and runs the first case that matches; case _: catches everything else.
Project: a tea-stall bill
Rahim runs a tea stall near a college. Tea is 15 taka a cup and a singara is 10 taka. At rush hour he adds up bills in his head, and sometimes he gets them wrong. He also wants to reward big orders: 5% off a bill of 100 taka or more, and 10% off a bill of 200 taka or more. In this project you'll write Rahim's bill program. It uses everything from World 1: input(), numbers, variables, f-strings and if/elif/else. The plan in one sentence: read the order, work out the costs, decide the discount, then print a neat bill. Its words: constant: a value that never changes while the program runs, like a price. Python's habit is to name it in CAPITALS: TEA = 15 input → process → output: the shape of almost every program: read something, work on it, show the result Step by step, the plan: 1. Read: ask for the number of cups and singaras, and turn the answers into numbers. 2. Work out: each cost is the count times the price; the total is their sum. 3. Decide: pick the discount with an if/elif/else chain, strictest rule first. 4. Print: show each line of the bill with an f-string, money with 2 decimal places. Here is step 3 for a 110 taka bill. Watch which rule wins: Steps 1 and 2: read and work out. Convert each answer with int() straight away, so everything after it works with numbers. Keep each cost in its own variable: the bill prints them, and the total adds them. Steps 3 and 4: decide and print. The chain checks total >= 200 before total >= 100: a 250 taka bill is also "100 or more", so the other order would always give the smaller discount. A percentage is total * 5 / 100, and / gives a float, so the money lines use :.2f. Why build it this way. Prices at the top. When tea goes up to 20 taka, you change one line, and every calculation follows. Work out first, print last. The numbers are ready before any line of the bill is shown, so the bill can't print a half-finished total. One chain for one decision. Exactly one discount rule applies, which is exactly what if/elif/else guarantees. Mistakes and edge cases. Forgetting int(). "2" * 15 doesn't raise an error: it repeats the text, 222222222222222. Convert the answers. The rules in the wrong order. With total >= 100 first, nobody ever gets 10% off. Percent as %. total % 10 is a remainder, not 10 percent. Write total * 10 / 100. Strange orders. A negative number of cups, or an empty order, should get a message instead of a bill. That's your third task.