Tallying result of an iterated loop

Twine Version: Harlowe 2.3.14

I’m pretty new to Twine, and I’m trying to figure out how to create a loop for that gives the player a number of prizes based on their overall score. Let’s say the player gets 5 prizes.

Each time the loop runs, a random number from 1 to 20 is generated. If the value is below 10, the player gets a cookie. If the value is above 10, the player gets a doughnut. If the value is 10 exactly, the player gets a cake. The loop count then increases by one, and the loop runs again until the count reaches the number of wins (5, in this case.)

At the end of the loop, I want to return to the story and give the result in a summary statement:

You earned $cookie cookies, $doughnut doughnuts, and $cake cakes!

I’ve tried writing this a number of ways, but I keep getting the same basic problem – I can’t figure out how to write a loop that runs, increments, and then tests the count to decide if it should exit and return. Any help with this would be most appreciated.

If I’m understanding what you’re describing, I don’t think you need to test a count to decide when to exit: you can just loop five times, using (for:) and (range:).

(set: $cookie to 0)(set: $doughnut to 0)(set: $cake to 0)
(for: each _i, ...(range: 1,5))[
	(set: _r to (random: 1,20))
	(if: _r < 10)[(set: $cookie to it + 1)]
	(elseif: _r > 10)[(set: $doughnut to it + 1)]
	(else:)[(set $cake to it + 1)]
]
You earned $cookie cookies, $doughnut doughnuts, and $cake cakes!