Constants and enums
A bare 50 or "gold" in the middle of a function is a magic value. The reader has to guess what it means, and changing it means finding every copy, including the one that is 50 for a different reason.
Give a number a name as a module-level constant, in capitals. For a fixed set of choices, use an Enum: a typo becomes an AttributeError instead of a silent mismatch, and editors can list the options.
from enum import Enum
VAT_RATE = 0.2
class Tier(Enum):
BASIC = "basic"
GOLD = "gold"
def with_vat(net):
return round(net * (1 + VAT_RATE), 2)
print(with_vat(10))
print(Tier("gold"), Tier.GOLD.value)Tier("gold") turns stored text back into a member. Compare members with is, since each one exists exactly once.
Not every number is magic: 0, 1 and len(items) - 1 usually explain themselves.
Your task
Name the numbers FREE_DELIVERY_FROM = 50, STANDARD_FEE = 4.99 and SILVER_FEE = 2.5, add an Enum called Tier with BASIC, SILVER and GOLD (values "basic", "silver" and "gold"), and make delivery_fee(total, tier) take a Tier and use the constants.
The explanation opens here once every check passes.