0

No matter the input, the function always prints "Enter a number".

def AddNum(n1, n2, n3, n4):
    while n1 and n2 and n3 and n4 == int:
        x = n1 + n2 + n3 + n4
        return x
    else:
        print("Enter a number.")
Zoey
  • 47
  • 3
  • https://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values might help you understand why constructs like `a and b and c == x` don't do what you think they do. – Zero Piraeus Oct 07 '17 at 23:51

1 Answers1

3

It's rather unclear why do you want a loop inside that, since a simple if statement would do the trick. Besides that, that's not how you do type checking - consider using isinstance(). Also, you might want your function to work with arbitrary number of arguments:

def add_num(*args):
    if all(isinstance(arg, int) for arg in args):
        return sum(args)
    else:
        return 'Arguments must be integers.'

...which could be additionally shortened to:

def add_num(*args):
    return sum(args) if all(isinstance(arg, int) for arg in args) else 'Arguments must be integers.'

>>> add_num('spam', 1)
Arguments must be integers.
>>> add_num(1, 2)
3
>>> add_num(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
55

You may as well want to read what PEP8 says about naming conventions when it comes to functions.

adder
  • 3,512
  • 1
  • 16
  • 28