Numbers & arithmetic · About 7 minutes

The cookie calculator

The big little idea

Python can do the math for your inventions. Use + to add, - to subtract, * to multiply, and / to divide. Numbers do not need quotation marks.

friends = 4
cookies_each = 3
print(friends * cookies_each)

Python uses the usual order of operations: multiplication and division before addition and subtraction. Parentheses let you choose what happens first. The // operator divides and rounds down to a whole number.

Predict, then try: read the example from top to bottom. What do you think it will print? Copy it into the editor below to test your prediction.

Your turn, code explorer.

You have 5 friends. Each gets 4 cookies. You bake 3 extra cookies for Bit. Use the variables to print the total: 23.

23
my_adventure.pyPython · in your browser

Edit the code. Then run it. Ctrl / ⌘ + Enter also works.

OutputYOUR IDEAS, ALIVE
Your program’s output will appear here.

Ready when you are. ✦

The first run loads Python. This can take a moment.

Program input For programs that use input()

Add answers before you run. Each input() reads the next line. Use made-up names and details.

Real Python, powered by Pyodide. Standard-library programs work here; desktop windows, turtle graphics, and package installation are not supported by this editor. Programs stop after 10 seconds of execution.

Back to the starting code?

This replaces your edits for this activity. Download your code first if you want to keep it.

A little hint, please

Multiply the number of friends by cookies_each first, then add extra.

Show one possible solution

There can be more than one way. Read this version, then try writing it yourself.

friends = 5
cookies_each = 4
extra = 3
print(friends * cookies_each + extra)
Pack this idea for later: A tiny formula can power a useful calculator.