Why there is "none" when i try to count odd numbers?

what i want:

count odd numbers of list a

my code as following:

def find_it(seq):
    set_seq=set(seq)
    dict_seq = {}
    for item in set_seq:
        dict_seq.update({item:seq.count(item)})
    print(dict_seq)

a=[20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5]
print(find_it(a))

This outputs:

{1: 2, 2: 2, 3: 2, 4: 2, 5: 3, 20: 2, -2: 2, -1: 2}
None

Why does it output None?

2 answers

  • answered 2018-11-08 07:26 Anagnostou John

    You do not return anything so there is nothing to be print. Here is the answer yo are looking for!!

    def find_it(seq):
        set_seq=set(seq)
        dict_seq = {}
        for item in set_seq:
            dict_seq.update({item:seq.count(item)})
        return dict_seq
    
    a=[20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5]
    my_dict = find_it(a)
    print(my_dict)
    

  • answered 2018-11-08 07:32 robotHamster

    Counting the odd numbers

    To count the odd numbers, after reducing to a set you should run the a loop looking at %2 append that number to a list (or increment a counter). Since you seem to be fairly new, here's an easy to understand approach:

    def find_it(seq):
        set_seq=set(seq)
        odds=[]
        for item in set_seq:
            if item%2==1:
                odds.append(item)
        return len(odds)