Numbers and arithmeticshare-the-cookies
LessonΒ·difficulty 1/5Β·~7 min
Share the cookies
Share the cookies
When you split things into equal groups, two questions come up: how many does each group get? and how many are left over? Python has one operator for each:
//β whole-number divide (how many fit in each group)%β the remainder (what's left over)
python
print(17 // 5) # 3
print(17 % 5) # 2
Your task
You have 17 cookies to share equally among 5 friends.
eachβ how many whole cookies each friend gets (cookies // friends).leftoverβ how many cookies are left over (cookies % friends).
python
cookies = 17
friends = 5
each = cookies // friends
leftover = cookies % friends
print(each, leftover)
Sanity check:
each * friends + leftovershould add right back up tocookies. That identity is true for any numbers you plug in.
Tests
- each friend gets 3 whole cookies
- 2 cookies are left over
- the pieces add back up to all the cookies (hidden)
Stuck? Ask for a spark
warming up
Loading editorβ¦
$ Console output will appear here.