Search code examples
pythonstringcountint

Python: count it int but python thinks it is str


I checked the "python" count function and found it to be integer, but when I try to check if it is greater than a certain number, it shows me that it is str

one = 0
two = 0
three = 0

for i in words:
    i = str(i)
    if (words.count(i) > one):
        one = i

    elif (words.count(i) > two):
        two = i

    elif (words.count(i) > three):
        three = i

The Error:

    if (words.count(i) > one):
TypeError: '>' not supported between instances of 'int' and 'str'

Solution

  • The problem is in the line:

    i = str(i)
    

    So later, when you assign a new value to one:

    one = i
    

    one becomes a string. Instead of changing i, change the if:

    if (words.count(str(i)) > one):