Mihai Stefanescu
Mihai Stefanescu

Reputation: 39

function gives me the error unorderable types: str() > int()

count_pos is supposed to input a list of numbers and return the quantity of positive values in that list.

def count_pos():
    numbers=list(input("Please enter a list of numbers separated by commas: "))
    count=0
    for number in numbers:
        if number > 0:
           count += 1
           add=sum(1 for number in numbers if number > 0)
           return add

When I input

-1, 2, -5, 0, 3

I get the error

Traceback (most recent call last):
  File "so.py", line 11, in <module>
    print (count_pos())
  File "so.py", line 6, in count_pos
    if number > 0:
TypeError: unorderable types: str() > int()

Upvotes: 0

Views: 709

Answers (4)

Prune
Prune

Reputation: 77847

Problem as given

The code you posted actually works in Python 2.7; it's Python 3 that doesn't have this list(input()) functionality.

Fortunately, your variable names are very clear. Others have detailed the immediate problem -- and I up-voted the explanation I think is particularly good. Let's fix this without damaging your program design:

def count_pos():
user_input = input("Please enter a list of numbers seperated by commas"))
# user_input is a raw string of the input, such as "1, 2, 3, 4"
# break this into individual numbers:
str_numbers = user_input.split(',')

# str_numbers would now be ["1", " 2", " 3", " 4"]
# These individual strings are suitable to convert to integer
numbers = [int(str_num) for str_num in str_numbers]

# NOW we can continue with the rest of your program, as written:
count=0
for number in numbers:
    if number > 0:
       count += 1
       add=sum(1 for number in numbers if number > 0)
       return add

Additional Clean-up

First of all, you return after you find the first positive number in your loop; count never gets higher than 1. However, you'll get the right answer, since your add= statement actually replaces what the loop would do. You can get rid of the loop entirely, and just use

return sum(1 for number in numbers if number > 0)

If you need to do this in a loop, then let it run to the end so it can do its job. Only then do you return the result:

for number in numbers:
    if number > 0:
       count += 1

return count

Editing note

Your original text says that count_pos "takes a list of numbers". This usually means that the calling program is responsible to supply that list, and your function must have an input parameter, rather than reading user input. In that case, your entire problem is solved by correcting the function header and removing the input and conversion, and using only your one-line solution:

def count_pos(numbers):
    return sum(1 for number in numbers if number > 0)

Upvotes: 1

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

because input will give you a string and string will be converted into collection of single string literals so if you enter

1 2 3

your list will be collection of strings , where space cannot be converted to int or compared

[' ', '1', ' ', '2', ' ', '3']

and within your if statement this is giving you the error at first comparison if ' ' > 0

so use this to convert your input into an integer list

numbers=list(map(int,input("Please enter a list of numbers seperated by commas").strip().split()))

Upvotes: 1

xZise
xZise

Reputation: 2379

The reason you get this specific error is actually that you do number > 0. The left side is a string (as I'll explain) and the right side is an integer. And Python doesn't allow you to compare strings and integers like that.

The list call doesn't work as you'd expect. input() returns a string, and if you do list("A_STRING") you'll get a list with each character (["A", "_"…]).

You probably want to look for str.split(), which separates a string with a given separator and returns you a list of strings. And then you actually need to convert each string in the list to a number (for example by using int(THE_STRING) or float(THE_STRING)).

So something like this:

text = input(…)
elements = text.split(",")
numbers = [int(element) for element in elements]

Upvotes: 0

B. Eckles
B. Eckles

Reputation: 1644

input returns a single string.

list(input(...)) returns a list with a single string in it.

for number in numbers: assigns number to that string, so it's now a string.

if number > 0 is then checking if the string in number is larger than 0.

And a string and an integer cannot be compared.

Presumably, what you want to do is change this line:

numbers=list(input("Please enter a list of numbers seperated by commas"))

To this:

numbers=map(float, list(input("Please enter a list of numbers seperated by commas").split(',')))

That will turn the string from input into a list of strings (it'll split up comma-separated values), and then map(float, ...) will apply the float() function to each element of that list to turn that list into a list of floats.

Upvotes: 1

Related Questions