11. Count : 'int' object is not iterable



Traceback (most recent call last):
File "python", line 10, in
File "python", line 3, in count
TypeError: 'int' object is not iterable

def count(sequence,item):
    found = 0
    for freq in sequence:
        if freq == item:
            found += 1
    return found

seq_list = [1,3,6,3,7,3,2,5,3,6,7,3,4]
seq_int = 3434345
print count(seq_list,3)
print count(seq_int,3)


remove this line:

print count(seq_int,3)

you can't loop over integers like this, it is what causes the error message, your function is fine, you shouldn't call it with arguments it can't handle


Thank You!
Can you please show me how to iterate over integers?


sure, we can:

integers = 123456789
total = 0
for i in str(integers):
    total += int(i)
print total

but that is outside the scope of the exercise. We can loop over strings, so we convert it to a string, allowing loop, then cast it to a integer to add to total.


Oh! Thanks for the help again!