Question #75360

2. Write a function moreOdds that accepts one argument, a list of integers. The function then returns a bool that indicates whether the list contains(strictly) more odd numbers than even numbers. Sample output:

>>> moreOdds( [2,3,4,5,7] )
True

Expert's answer

Question

Write a function moreOdds that accepts one argument, a list of integers. The function then returns a bool that indicates whether the list contains (strictly) more odd numbers than even numbers. Sample output:


>>> moreOdds( [2,3,4,5,7] )
True

Answer:

def moreOdds(lst):
    odds = 0
    evens = 0
    for n in lst:
        if n % 2 == 0:
            evens += 1
        else:
            odds += 1
    if odds > evens:
        return True
    return False
LATEST TUTORIALS
APPROVED BY CLIENTS