Only integers

Define a function add_only_integers that receives two numbers and returns
the sum of them ONLY if they’re both integers. If that’s not the case, the
function should return the string "invalid parameters".

Hint: You might find useful functions like isinstance or type.


>>> add_only_integers(2, 3)
>>> add_only_integers(2, 'what')
'invalid parameters'

Test Cases

test not integers - Run Test

def test_not_integers():
    assert add_only_integers(2, 'what') == 'invalid parameters'

test integers - Run Test

def test_integers():
    assert add_only_integers(2, 3) == 5

Solution 1

def add_only_integers(a, b):
    if isinstance(a, int) and isinstance(b, int):
        return a + b
    return 'invalid parameters'

# Alternatively:
# (please note how we're inverting the boolean operator `and` vs `or`.

# def add_only_integers(a, b):
#     if type(a) != int or type(b) != int:
#         return 'invalid parameters'
#     return a + b
def add_only_integers(a, b): pass