Python:把它算作int,但python认为它是str。

问题描述 投票:0回答:1

我检查了 "python "的count函数,发现它是整数,但当我试图检查它是否大于某个数字时,它显示我是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

的错误。

    if (words.count(i) > one):
TypeError: '>' not supported between instances of 'int' and 'str'
python string count int
1个回答
1
投票

问题就出在这一行。

i = str(i)

所以后来,当你把一个新的值赋给: one:

one = i

one 变成一个字符串。 而不是改变 i,改变 if:

if (words.count(str(i)) > one):
© www.soinside.com 2019 - 2024. All rights reserved.