类型错误:“str"和“int"的实例之间不支持“<=" [英] TypeError: '<=' not supported between instances of 'str' and 'int'

查看:62
本文介绍了类型错误:“str"和“int"的实例之间不支持“<="的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Python 和练习.其中之一是编写投票系统以使用列表在比赛的 23 名球员中选择最佳球员.

I'm learning python and working on exercises. One of them is to code a voting system to select the best player between 23 players of the match using lists.

我正在使用 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: '<=' 在 'str' 和 'int' 的实例之间不受支持

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

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

But I don't have any strings here, all variables are integers.

推荐答案

更改

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 对象才能进行数值运算.

You are getting the input from the console as a string, so you must cast that input string to an int object in order to do numerical operations.

这篇关于类型错误:“str"和“int"的实例之间不支持“&lt;="的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆