Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A function is_even(n) should say whether a whole number is even. Write six assert lines that test it, including zero, a negative number, and a big number. Write the expected answers from what you know about even numbers.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Use the rule: a whole number is even if it divides by 2 with no remainder. Choose a spread of cases, with the edges: zero and the negatives.
Check. Zero is even: 0 divided by 2 is 0, with no remainder. In Python, -7 % 2 is 1, so -7 is odd, and -4 % 2 is 0, so -4 is even.
Answer: The six asserts above all pass, and the program prints all six passed.
Log in to join the discussion.
Loading…