0

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?

khelwood
  • 55,782
  • 14
  • 81
  • 108
CZ_want2b
  • 13
  • 4

2 Answers2

0

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)
Anagnostou John
  • 498
  • 5
  • 14
0

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)
robotHamster
  • 609
  • 1
  • 7
  • 24