Exercises
Numbers
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Print how many whole times 3 goes into 17, and what is left over.
Python
total = 17
per_box = 3
# print full boxes, then the remainder
// gives the whole number of times, % gives the leftover.
total = 17
per_box = 3
print(total // per_box)
print(total % per_box)Exercise 2Passed
0.1 + 0.2 does not equal 0.3 exactly. Print True by comparing them with a tolerance instead.
Python
# compare 0.1 + 0.2 with 0.3 safely
math has a function called isclose.
from math import isclose
print(isclose(0.1 + 0.2, 0.3))Exercise 3Passed
Print every even number below 10, one per line, using the remainder operator.
Python
for n in range(10):
# print n only when it is even
passA number is even when dividing by 2 leaves nothing.
for n in range(10):
if n % 2 == 0:
print(n)