The request

From Debra Marshall, Warehouse Manager
Your goal
Set result to a dictionary with keys 'weeks' (an int: how many full weeks of demand the stock covers, where week 1 needs demand units and each later week needs 10% more than the week before) and 'left' (units left after those weeks, a float rounded to 1).
- Store a dict 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 Debra gets, and why
The lesson
for or while?
A for loop walks through something that already exists: a list of orders, the keys of a dictionary,
a range. A while loop repeats as long as a condition is true, which is what you need when
you don’t know in advance how many steps there will be.
while stock >= demand:
# one more week can be covered
...
Python checks the condition, runs the body, checks again, and stops the first time the condition is false. The body must change something the condition depends on, or the loop never ends.
Counting weeks with growing demand
If demand were flat, 260 // 38 would answer the question: 6 weeks. But holiday demand grows 10%
each week, so every week needs more than the last. The loop handles that by updating three things on
every pass:
weeks = 0
while stock >= demand:
stock = stock - demand # ship this week's units
demand = demand * (1 + growth) # next week needs 10% more
weeks += 1 # one more week covered
Tracing it shows how quickly growth eats the buffer. The weekly needs are 38, 41.8, 45.98, about
50.6 and about 55.6. After five weeks 232 units have gone out and 28 are left. Week six would need
about 61.2, more than what remains, so the condition is false and the loop stops with weeks at 5.
That is a full week less than the flat-demand estimate, which matters for a January delivery.
Make sure it can stop
Every while loop needs a way out. Here the stock falls and the demand rises on each pass, so
stock >= demand must eventually become false. If you forget to subtract the demand, the stock never
changes and the loop runs until the page stops it. When you write a while, ask “what in the body
moves the condition towards false?” before you run it.
Common mistakes
- Using floor division on the starting numbers and ignoring growth.
- Forgetting one of the updates, which gives an endless loop.
- Testing
stock > 0instead ofstock >= demand. That counts a sixth week the stock cannot cover. - Rounding
demandon every pass, which slowly drifts away from the real numbers.
Lantern lit!
Practised: while loopaccumulatorloop conditiongrowth
- Solved
- No hints
- First run
