Making decisions
A boolean on its own only reports. if lets the program act on it: run
some lines only when a condition is true.
stock = 0
if stock == 0:
print("Sold out")
print("Done")
The if line ends with a colon. The lines indented under it are the
block, and they run only when the condition is True. print("Done") is
not indented, so it is not part of the block and always runs. Change stock
to 5 and run again: "Sold out" disappears, "Done" stays.
Otherwise
else gives the other path. Exactly one of the two blocks runs.
order_total = 60
if order_total >= 50:
print("Free delivery")
else:
print("Delivery costs 5")
More than two paths
elif is short for "else if". Python checks each condition from the top and
runs the first block whose condition is true, then skips the rest.
quantity = 12
if quantity >= 10:
print("Bulk discount")
elif quantity >= 5:
print("Small discount")
else:
print("No discount")
With quantity = 12, both quantity >= 10 and quantity >= 5 are true, but
only the first block runs. Order matters: if the >= 5 check came first, the
bulk discount could never be reached.
Returning from inside an if
Inside a function, return ends the function immediately. That makes a
common shape very clean:
def delivery_fee(order_total):
if order_total >= 50:
return 0
return 5
print(delivery_fee(60))
print(delivery_fee(20))
No else is needed. If the if returns, the second return is never
reached; if it does not, the function falls through to it.
Try it
Delivery fee
Delivery costs 5, but it is free for orders of 50 or more. Write
delivery_fee(total) that returns the fee.
delivery_fee(60) -> 0
delivery_fee(50) -> 0
delivery_fee(20) -> 5
def delivery_fee(total):
...
Discount rate
The shop gives a discount based on quantity: 10 or more items get 20%,
5 to 9 items get 10%, fewer than 5 get nothing. Write discount_rate(quantity)
that returns the rate as a number: 0.2, 0.1, or 0.
discount_rate(12) -> 0.2
discount_rate(5) -> 0.1
discount_rate(2) -> 0
def discount_rate(quantity):
...
Clamp a quantity
Orders must be for at least 1 and at most 20 of an item. Write
clamp_quantity(q) that returns q unchanged if it is already in range,
1 if it is below, and 20 if it is above.
clamp_quantity(7) -> 7
clamp_quantity(0) -> 1
clamp_quantity(99) -> 20
def clamp_quantity(q):
...
Next: Strings: looking inside.