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

I am learning python and working on exercises. One of them is to encode a voting system in order to select the best player between 23 players of a match using lists.

I use Python3.

My code is:

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: '<=' is not supported between instances of 'str' and 'int'

But I have no lines, all variables are integers.

+18
source share
4 answers

Edit

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 get input from the console as a string, so you must make this input string to an object intto perform numerical operations.

+26

Python3.x input, , int .

Python3

, . , ( ) . EOF , EOFError .

, try catch, int:

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

, .

+13

, . :

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

which turns input into an int value

+1
source

input () by default accepts input as strings.

if (0<= vote <=24):

Voting takes string input (suppose "4", "5", etc.) and becomes disparate.

Correct way: vote = int(input("Enter your message")converts input to an integer (from 4 to 4 or from 5 to 5 depending on input)

0
source

Source: https://habr.com/ru/post/1014576/


All Articles