-2

I'm trying to see if a list contains integers:

def fun(a):
    if a[:] == int:
        print('Sum of elements', sum(a))
        print('Minimum number ', min(a))
        print('Maximum number ', max(a))
        print('Length of elements', len(a))
    else:
        print('Give integers list only')

list = [1,2,3,4,5,'hi']
fun(list) 

Output:

**Output :** *Give integers list only*
Algebra8
  • 1,115
  • 1
  • 10
  • 23
Ali
  • 35
  • 1
  • 5

3 Answers3

1

You need to check type item by item. Replace if a[:]==int: with

if all(isinstance(n, int) for n in a):
    ...

in your definition.

mathfux
  • 5,759
  • 1
  • 14
  • 34
0

You can just loop through it and use is_integer() If at any point it fails then return an error

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
TheLovelySausage
  • 3,838
  • 15
  • 56
  • 106
0
def fun(a):
    for value in a:
        if not isinstance(value, int):
            print('Give integers list only')
            break 
    else:
        print('Sum of elements', sum(a))
        print('Minimum number ', min(a))
        print('Maximum number ', max(a))
        print('Length of elements', len(a))
Algebra8
  • 1,115
  • 1
  • 10
  • 23