+-
python – TypeError:’str’和’int’实例之间不支持’<='
参见英文答案 > How can I read inputs as numbers?                                    10个
我正在学习python并且正在练习练习.其中之一是编码投票系统,使用列表选择比赛的23名球员之间的最佳球员.

我正在使用Python3.

我的代码:

players= [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
vote = 0
cont = 0

while(vote >= 0 and vote <23):
    vote = input('Enter the name of the player you wish to vote for')
    if (0 < vote <=24):
        players[vote +1] += 1;cont +=1
    else:
        print('Invalid vote, try again')

我明白了

TypeError: ‘<=’ not supported between instances of ‘str’ and ‘int’

但我这里没有任何字符串,所有变量都是整数.

最佳答案
更改

vote = input('Enter the name of the player you wish to vote for')

vote = int(input('Enter the name of the player you wish to vote for'))

您将从控制台获取输入作为字符串,因此必须将该输入字符串转换为int对象才能执行数值运算.

点击查看更多相关文章

转载注明原文:python – TypeError:’str’和’int’实例之间不支持’<=' - 乐贴网