Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

I'm using 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'

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

like image 300
Douglas da Dias Silva Avatar asked Jan 31 '17 05:01

Douglas da Dias Silva


People also ask

How do I fix TypeError not supported between instances of Nonetype and int?

To solve the error, you have to figure out where the None value comes from and correct the assignment or conditionally check if the variable doesn't store None . The most common sources of None values are: Having a function that doesn't return anything (returns None implicitly). Explicitly setting a variable to None .

Is not supported between instances?

The Python "TypeError: '>' not supported between instances of 'method' and 'int'" occurs when we use a comparison operator between a method and an integer. To solve the error, make sure to call the method with parenthesis, e.g. my_method() . Here is an example of how the error occurs. Copied!

Can only concatenate str not TypeError to STR?

The Python "TypeError: can only concatenate str (not "list") to str" occurs when we try to concatenate a string and a list. To solve the error, access the list at a specific index to concatenate two strings, or use the append() method to add an item to the list.


2 Answers

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 int object in order to do numerical operations.

like image 65
X33 Avatar answered Oct 11 '22 02:10

X33


If you're using Python3.x input will return a string,so you should use int method to convert string to integer.

Python3 Input

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.

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

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

Hope this helps.

like image 21
McGrady Avatar answered Oct 11 '22 02:10

McGrady