Why not go a step further and implement this as a class, this way you learn something about magic methods as well :)
class Fingers(int):
"""A class implementing counting with fingers instead of the more effective built-in int methods"""
def __init__(self, x):
if not isinstance(x, int):
raise TypeError("Input must be 'int' (or subclass)")
self.x = x
def __pos__(self):
return self
def __neg__(self):
return Fingers(-self.x)
def __abs__(self):
return Fingers(abs(self.x))
def __add__(self, other):
"""a + b, a and b may be negative"""
y_add = (1 if other >= 0 else -1)
x = self.x
for _ in range(abs(other)):
x += y_add
return Fingers(x)
def __mul__(self, other):
"""a * b, a and b may be negative"""
m = Fingers(0)
function = m.__add__
for _ in range(abs(other)):
m = m + self
return m
def __pow__(self, exponent):
"""a**b, b must be positive"""
if exponent < 0:
raise NotImplementedError
p = Fingers(1)
for _ in range(y):
p = p * self
return p
def __divmod__(self, other):
floor = Fingers(0)
remainder = Fingers(0)
for _ in range(self):
remainder = remainder + Fingers(1)
if remainder == other:
floor = floor + Fingers(1)
remainder = Fingers(0)
return floor, remainder
def __floordiv__(self, other):
"""a//b"""
return self.__divmod__(other)[0]
def __mod__(self, other):
"""a%b"""
return self.__divmod__(other)[1]
if __name__ == "__main__":
x = Fingers(3)
y = Fingers(2)
assert x + y == Fingers(5)
assert x * y == Fingers(6)
assert x**y == Fingers(9)
assert divmod(x, y) == (Fingers(1), Fingers(1))
assert x // y == Fingers(1)
assert x % y == Fingers(1)
(I did not implement __iadd__ etc, so x += 3 will not work. They are trivial to implement, though.)