In this Python coding challenge your goal is to implement the modulo operator (`%`

) for yourself, without using division or the `%`

operator itself. We’ll only consider positive integers here, although by all means go ahead and create a version that can handle negative ones too.

Sometimes the goal is to get something done and so we use existing tools available to do that, but other times the goal is different – perhaps we want to understand something better, or even just enjoy the thrill of solving a problem for its own sake, knowing that in doing so we are adding to our personal database of experience, making us better programmers.

Today’s challenge is to implement the **modulo operator** as if it didn’t already exist.

The modulo operator is used in Python programming when you need to find the remainder of integer division. It comes up in many important algorithms, and is represented by the symbol

`%`

. The`%`

operator gives the result of dividing the number on the left of the symbol by the number on the right and keeping the remainder.

For a detailed lesson on the modulo operator, complete with Python code, worksheets and solutions, check out my Modulo Operator Resource.

[product id=”4961″]

Before getting stuck into the solution in Python, it might be a good idea to make sure your really understand how the modulo operator works by trying a few examples on paper. Then think carefully about your plan of attack, perhaps making a few notes/bullet points.

## Python Modulo Operator Challenge

Here’s a stub and some basic tests to get you started once you think you are ready to implement your solution in Python:

```
def modulo2(a, b):
"""
Returns a % b without using division or modulo operator.
a, b must be integers. a must be >= 0 and b must be > 0.
"""
pass
assert modulo2(20, 6) == 2
assert modulo2(0, 6) == 0
assert modulo2(123, 10) == 3
```

## Python Modulo Operator solution

Here’s a potential solution. Yours may well be quite different, but if it passes the tests, it’s probably OK (although it’s certainly worth adding a few more tests of your own).

## Improving the solution

One thing that we can do to improve the solution is to add some validation. Since `a`

and `b`

must be integers and `a`

must be `>= 0`

and `b`

must be `> 0`

, we can modify the program to raise an error if these conditions are not met. However using simple assert statements is no longer an option for testing this kind of input.

```
def modulo2(a, b):
"""
Returns a % b without using division or modulo operator.
a, b must be integers. a must be >= 0 and b must be > 0.
"""
if not(type(a) is int and type(b) is int):
raise ValueError("a and b must both be integers.")
if not(a >= 0 and b > 0):
raise ValueError("a must be >= 0 and b must be > 0.")
return False
while a >= b:
a -= b
print(f"a: {a}, b: {b}") # For debugging
return a
assert modulo2(20, 6) == 2
assert modulo2(0, 6) == 0
assert modulo2(123, 10) == 3
# Testing for ValueError requires a testing framework
print(modulo2(-10, 2)) # Expect ValueError
```

I hope you found this challenge interesting. Happy computing!