Exercise 3 of 10 · Arithmetic
Party Planner
What you will make
A pizza order summary that works itself out from a guest list — slices needed, whole pizzas, and the slices left over.
The one new idea: Integer division truncates with /, and % gives the remainder
Counting things into whole groups comes up everywhere: pizzas, boxes, minutes into hours. Ruby has an operator for each half of that question, so the sum never has to be done on paper.
Go straight to the code ↓Two kinds of division
Ruby does ordinary arithmetic with +, - and *, working roughly as you would on paper. Division is where it makes a choice most calculators do not.
Picture nineteen sweets shared between four friends. There are two sensible answers, not one. The first is "four each" — the whole sweets everyone actually gets. The second is "three left in the bag" — the ones that could not be shared out evenly. Ruby has an operator for each:
/between two whole numbers counts the whole groups, so19 / 4is4%reports what is left over, so19 % 4is3
This is worth pausing on, because it differs from some languages you may meet later: in Ruby, a single / between two integers already rounds down to a whole number. There is no separate "integer division" symbol to remember — the moment either side of the / is a decimal, though, the whole calculation becomes a decimal too.
A worked example
sweets = 19
friends = 4
each = sweets / friends
left = sweets % friends
puts "Sweets each .. #{each}"
puts "Left in bag .. #{left}"Sweets each .. 4
Left in bag .. 3Two operators, the same pair of numbers, two different and equally true answers.
Your turn
The editor has a party to plan: twenty-six guests, three slices each, and a pizza that comes cut into eight. The program already multiplies the guests by the slices to get the total number of slices wanted. Two lines are unfinished:
pizzas = 0
extra = 0Replace each 0 with a sum built from slices_needed and per_pizza. Give pizzas the whole pizzas the order fills, and extra the slices spilling past them.
Run it and read the summary back. If the last two numbers still show 0, nothing has changed yet — the program runs happily with zeros in it, it just plans a party with no food.
If something goes wrong
The likeliest slip is a number turning up with a decimal point where a plain count was expected — that means a plain / gave a fractional answer, which only happens if a stray .0 crept into one of the variables above. Delete the dot; both numbers in this program stay whole.
The other thing worth checking is the two operators landing the wrong way round. Nothing breaks and the block still prints, but a leftover of 8 or more, or a pizza count that cannot possibly feed the guest list, is the giveaway.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Expecting / to give a decimal
- With two whole numbers, Ruby's / rounds down to a whole number on its own — 78 / 8 is 9, not 9.75. There is no separate operator to ask for like some other languages; a plain / already does it, as long as both sides are integers.
- Letting a decimal point sneak into one of the numbers
- per_pizza = 8.0 turns / into ordinary decimal division the moment Ruby sees it, so pizzas ends up holding 9.75 instead of 9. Keep every number in this program a whole number, with no dot in it.
- Putting the two operators the wrong way round
- Nothing breaks and the block still prints, but the numbers stop making sense: 6 pizzas cannot come from a leftover of 9 slices, and a leftover should never reach 8 or more, since 8 slices is a whole pizza on its own.
- Deleting the 0 and leaving the line unfinished
- A line reading pizzas = with nothing after it is an unfinished sentence. Ruby stops and reports a syntax error, pointing at that line.
Want a blank editor instead? Open the Ruby playground.