Home > Net >  how to check if a given list of numbers are odd or even?
how to check if a given list of numbers are odd or even?

Time:01-12

I want to check the numbers in a list are even or odd in order and if not tell me which one. for example : 1,2,3,4,5 IS OK but: 1,2,8,3,4,5 number 8 IS NOT OK

I tried to make the list into 2 different lists and then check if the are all odd or all even but I can't figure out how to check both of them.

lst = [1,2,3,4,5,6,7,8,9,10]
m = lst[::2]
w = lst[1::2]

for i, j in(m,w):
    if i  % 2 == 0 and j % 2 == 0:

CodePudding user response:

What I understood from your question is you want odd numbers on even positions and even numbers on odd positions:

def func(l):
    c=0   #counter variable
    for x,y in enumerate(l):
        if x%2==0:
            if y%2!=0:
                continue
            else:
                c =1
                break
    if c>0:
        return 'NOK'
    else:
        return 'OK'

print(func([1,2,3,4,5]))
#'OK'

print(func([1,2,8,3,4,5]))
#'NOK'

CodePudding user response:

Try with

list(map(lambda el: 'odd' if el%2 else 'even', lst))
  • Related