
Code
Whole Jars and Leftovers
Sometimes a decimal answer is no good. You can't give an island 3.4 jars of oil! Python has two operators for sharing things out in whole pieces:
| Operator | Asks | Example | Result |
|---|---|---|---|
// |
how many whole times does it fit? | 7 // 2 |
3 |
% |
what is left over? | 7 % 2 |
1 |
// is called whole-number division (or floor division).
% is called the remainder. They make a great team:
cookies = 10
friends = 3
print(cookies // friends) # 3 each
print(cookies % friends) # 1 left over
Check it: 3 friends get 3 cookies each. That's 9, plus 1 left over, makes 10.
Assignment
The Guild has 17 jars of lantern oil to share between 5
islands. Use the variables jars and islands, not the
numbers:
- Set
eachto how many whole jars each island gets. - Set
left_overto how many jars remain.
The Guild has 17 jars of lantern oil to share between 5
islands. Use the variables jars and islands, not the
numbers:
- Set
eachto how many whole jars each island gets. - Set
left_overto how many jars remain.
main.py Python sleeping
Press Run to try your code, then Check to test it.