The request

From Thomas Whitaker, Head of Merchandising
Your goal
Set result to the gross margin percent of the sale as a float rounded to 1 decimal: revenue is quantity * price * (1 - discount_pct / 100), cost is quantity * unit_cost, and margin percent is (revenue - cost) / revenue * 100.
- Store a number in
resultnot done yet
Your code
Loading the editor…
Reference solution
Mote checks your result
Press Run. Each row that matches the goal becomes a firefly in your lantern.
What Thomas gets, and why
The lesson
Revenue, cost and profit
Every sale has two sides. Revenue is what the customer paid us, after any discount. Cost is what the goods cost us to buy. The difference is gross profit.
revenue = 2 * 919.00 * (1 - 15 / 100) # 1562.3
cost = 2 * 503.50 # 1007.0
profit = revenue - cost # 555.3 (give or take a float)
The discount is stored as a whole number of percentage points, 15, so it has to be divided by 100
before you subtract it from 1. 1 - 15 / 100 is 0.85: the customer pays 85% of the list price.
Python does the division before the subtraction, the same order you learned at school, so no extra
parentheses are needed inside.
Storing each step in a named variable costs nothing and makes the calculation easy to read and to check. Anyone reviewing it can see where 1,562.30 came from.
Margin as a percent
Gross margin percent is profit divided by revenue, times 100. It answers “of every dollar the customer paid, how many cents did we keep?”
margin_pct = profit / revenue * 100 # 35.543...
round(margin_pct, 1) # 35.5
round(x, 1) keeps one decimal. Round only at the very end. If you round revenue and cost first and
then divide, small rounding errors can move the last digit of the answer.
Margin is not markup
Divide the same profit by cost instead and you get markup: 555.30 / 1,007.00 is about 55.1%. Both numbers are real and both get used, but they answer different questions. Margin compares profit with the price; markup compares profit with what we paid. Buyers often think in markup, finance reports in margin. When someone asks for “the margin”, they mean profit over revenue.
A quick sense check helps here: a margin can never be above 100%, because profit can never be more than the price. A markup can be anything.
Common mistakes
- Using the list price instead of the discounted price, which hides the cost of the discount. Without the discount the margin would be 45.2%, almost ten points better than reality.
- Writing
1 - discount_pctinstead of1 - discount_pct / 100. That multiplies by -14 and gives a negative revenue. - Dividing by cost and reporting markup as margin.
- Rounding in the middle of the calculation instead of once at the end.
Lantern lit!
Practised: variablesarithmeticroundpercentages
- Solved
- No hints
- First run
