Python 类型错误:“str”和“int”的实例之间不支持“<=”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41950021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:55:09  来源:igfitidea点击:

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

pythonpython-3.x

提问by Douglas da Silva

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.

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

I'm using Python3.

我正在使用Python3.

My code:

我的代码:

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')

I get

我得到

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

类型错误:“str”和“int”的实例之间不支持“<=”

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

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

回答by X33

Change

改变

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

to

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

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

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

回答by McGrady

If you're using Python3.x inputwill return a string,so you should use intmethod to convert string to integer.

如果您使用的是 Python3.xinput将返回一个字符串,因此您应该使用int方法将字符串转换为整数。

Python3 Input

Python3 输入

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string(stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

如果存在 prompt 参数,则将其写入标准输出,而没有尾随换行符。然后该函数从输入中读取一行, 将其转换为字符串(去除尾随的换行符),然后返回该字符串。读取 EOF 时,会引发 EOFError。

By the way,it's a good way to use trycatchif you want to convert string to int:

顺便说一句,trycatch如果您想将字符串转换为 int ,这是一个很好的使用方法:

try:
  i = int(s)
except ValueError as err:
  pass 

Hope this helps.

希望这可以帮助。

回答by R. Mercy

When you use the input function it automatically turns it into a string. You need to go:

当您使用 input 函数时,它会自动将其转换为字符串。你需要去:

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

which turns the input into a int type value

它将输入转换为 int 类型值

回答by Drool

input() by default takes the input in form of strings.

input() 默认采用字符串形式的输入。

if (0<= vote <=24):

vote takes a string input (suppose '4','5',etc) and becomes uncomparable.

投票需要一个字符串输入(假设'4'、'5'等)并且变得无法比较。

Correct way is : vote = int(input("Enter your message")will convert the input to integer ('4' to 4 or '5' to 5 depending on the input)

正确的方法是:vote = int(input("Enter your message")将输入转换为整数('4' 到 4 或 '5' 到 5,具体取决于输入)