11. Count : 'int' object is not iterable


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/1?curriculum_id=4f89dab3d788890003000096#


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)

#2

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


#3

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


#4

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.


#5

Oh! Thanks for the help again!