Loading…
When you see repeated patterns, extract them into functions:
# Calculate discounted prices
price1 = 100
discount1 = price1 * 0.1
final1 = price1 - discount1price2 = 200
discount2 = price2 * 0.1
final2 = price2 - discount2
def apply_discount(price, rate=0.1):
return price - (price * rate)final1 = apply_discount(100)
final2 = apply_discount(200)
The starter code calculates totals with tax repetitively. Create a function calculate_with_tax and use it to simplify the code.
Downloading Python… (one-time, ~10 MB)
This runs entirely in your browser and is cached for next time.