Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the Unorderable Type error mean in Python?

from urllib.request import urlopen
page1 = urlopen("http://www.beans-r-us.biz/prices.html")
page2 = urlopen("http://www.beans-r-us.biz/prices-loyalty.html")
text1 = page1.read().decode("utf8")
text2 = page2.read().decode("utf8")
where = text2.find(">$")
start_of_price = where + 2
end_of_price = where + 6
price_loyal = text2[start_of_price:end_of_price]
price = text1[234:238]
password = 5501
p = input("Loyalty Customers Password? : ")
passkey = int(p)

if passkey == password:
    while price_loyal > 4.74:
        if price_loyal < 4.74:
            print("Here is the loyal customers price :) :")
            print(price_loyal)
        else:
            print( "Price is too high to make a profit, come back later :) ")
else:
    print("Sorry incorrect password :(, here is the normal price :")
    print(price)
input("Thanks for using our humble service, come again :), press enter to close this window.")

The problem I'm having is that it runs until I get the the 4.74 part. Then it stops and complains about an unorderable type. I'm completely confused as to what that means.

like image 941
Humility Avatar asked May 28 '12 03:05

Humility


1 Answers

price_loyal is a string (even if it contains numbers that you have found with find) that you are trying to compare to a numeric value (4.75)? For your comparison try

float(price_loyal)

UPDATE (thanks @agf):

With Python v 3.x you get the error message you mentioned.

>>> price_loyal = '555.5'
>>> price_loyal  > 5000.0
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    price_loyal > 5000.0
TypeError: unorderable types: str() > float()
>>> 

whereas

>>> float(price_loyal) > 5000.0
False

The version of Python makes a difference in this case, so probably a good idea to always mention what version one is working with. Previously ... with Python v 2.x

Your comparisons will be off without converting your string to a float first. E.g.,

price_loyal
'555.5'

This comparison with string and float gives True

price_loyal > 5000.0
True

This comparison with float and float gives False as it should

float(price_loyal) > 5000.0
False

There might be other problems, but this looks like one.

like image 154
Levon Avatar answered Oct 21 '22 03:10

Levon