Exercise 3 of 10 · Arithmetic
Party Planner
What you will make
A pizza order summary that works itself out from the guest list — slices needed, whole pizzas, and the slices that spill past them.
The one new idea: Python does arithmetic with + - * // and %
Counting things into whole groups turns up everywhere: pizzas, pages, boxes, minutes into hours. Python has an operator for each half of that question, so you never have to do the sum on paper.
Go straight to the code ↓Arithmetic, and two kinds of division
Python does sums. You write them roughly as you would on paper, with a couple of symbols swapped out:
+adds-subtracts*multiplies, using a star rather than an x
So guests * slices_each means the guests multiplied by the slices each, and Python works the answer out
the moment it reads that line. Nothing is stored on paper and nothing is rounded behind your back.
Division is where Python hands you a choice, and that choice is what this exercise is about.
Picture seventeen apples shared between five friends. There are two sensible answers, not one. The first is "three each" — how many whole apples everybody walks away with. The second is "two left in the bowl" — the ones that could not be shared out evenly. Python gives you an operator for each:
//counts the whole groups, so17 // 5is3%reports what is left over, so17 % 5is2
You may know % as the percent sign. In Python it has nothing to do with percentages; here it means
remainder, the part that does not fit. Both operators hand back plain whole numbers, which is exactly what
you want when the things being counted cannot sensibly be cut in half — apples, chairs, boxes, pizzas.
A worked example
apples = 17
friends = 5
each = apples // friends
over = apples % friends
print("Apples each ..", each)
print("Left in bowl .", over)Running that prints two lines:
Apples each .. 3
Left in bowl . 2Two operators, the same pair of numbers, two different and equally true answers. The rows of dots are
nothing clever — they are part of the text inside the quotes, doing the job of the ruled line on a paper
form. The comma inside print sets the label and the number down on one line with a single space between.
Your turn
The editor has a party to plan. Twenty-three guests are coming, each one will eat three slices, and a pizza turns up cut into eight.
The program already multiplies the guests by the slices to get the total number of slices wanted, and it already prints the whole summary block. Two lines are unfinished:
pizzas = 0
extra = 0Replace each 0 with a sum built from slices_needed and per_pizza. Give pizzas the number of whole
pizzas the order fills, and extra the slices that spill past them.
Then run it and read the block back. If the last two numbers still show 0, nothing has been changed yet — the program runs perfectly happily with zeros in it, it just plans a party with no food.
When it works you get a genuine answer: eight whole pizzas cover sixty-four slices, and five slices are still wanted on top. In real life you would order the ninth pizza and have three slices going spare.
If something goes wrong
The likeliest slip is one slash where two were meant. 69 / 8 gives 8.625, which is a perfectly good
answer to a maths question and a hopeless answer to a pizza question. If a number turns up with a dot in
the middle of it where you expected a plain count, the missing slash is why.
The other thing worth watching is the two operators landing the wrong way round. Nothing breaks and the block prints as neatly as ever, but the figures stop hanging together. Leftover slices must always come to fewer than eight — if that line reads 8 or more, there is another whole pizza hiding inside it.
Change it, run it, change it again. Nothing here can break.
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
- Typing one slash instead of two
- A single slash gives 8.625 rather than 8. It is a fair answer to a division sum and a useless answer to a pizza question, because nobody sells you 0.625 of a pizza.
- Putting the two operators the wrong way round
- Nothing breaks and the block still prints, but the numbers stop making sense: 5 pizzas cannot feed 23 people, and leftover slices should never reach 8, because 8 slices is a whole pizza.
- Deleting the 0 and leaving the line unfinished
- A line reading pizzas = with nothing after it is an unfinished sentence. Python stops, reports a syntax error, and points at that line.
Longer explanation: read the full lesson. Want a blank editor instead? Open the Python playground.