Skip to content Skip to sidebar Skip to footer

"invalid Literal For Int() With Base 10:" What Does This Actually Mean?

Beginner here! I am writing a simple code to count how many times an item shows up in a list (ex. count([1, 3, 1, 4, 1, 5], 1) would return 3). This is what I originally had: def

Solution 1:

The error is "invalid literal for int() with base 10:". This just means that the argument that you passed to int doesn't look like a number. In other words it's either empty, or has a character in it other than a digit.

This can be reproduced in a python shell.

>>> int("x")
ValueError: invalid literal forint() with base 10: 'x'

Solution 2:

You could try something like this if letters for example, occur in your sequence:

from __future__ import print_function

defcount_(sequence, item):
    s = 0for i in sequence:
        try:
            ifint(i) == int(item):
                s = s + 1except 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))

Post a Comment for ""invalid Literal For Int() With Base 10:" What Does This Actually Mean?"