"Invalid literal for int () with base 10:" What does this mean?

Beginner is here! I am writing simple code to calculate how many times an item is displayed in a list (e.g. count([1, 3, 1, 4, 1, 5], 1)returns 3).

This is what I originally had:

def count(sequence, item):
    s = 0
    for i in sequence:
       if int(i) == int(item):
           s += 1
    return s

Every time I sent this code, I received

"invalid literal for int () with base 10:"

Since then, I realized that the correct code is:

def count(sequence, item):
    s = 0
    for i in sequence:
       if **i == item**:
           s += 1
    return s

However, I'm just wondering what this error expression means. Why can't I just leave in int()?

+4
source share
2 answers

- " int() 10:". , , int, . , , , .

python.

>>> int("x")
ValueError: invalid literal for int() with base 10: 'x'
+8

- , , , :

from __future__ import print_function

def count_(sequence, item):
    s = 0
    for i in sequence:
        try:
            if int(i) == int(item):
                s = s + 1
        except ValueError:
            print ('Found: ',i, ', i can\'t count that, only numbers', sep='')
    return s

print (count_([1,2,3,'S',4, 4, 1, 1, 'A'], 1))
0

All Articles