What does 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 am facing is that it works until I get part 4.74. Then he stops and complains about the disordered type. I am completely confused as to what this means.

+6
source share
2 answers

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

float(price_loyal)

UPDATE (thanks @agf):

With Python v 3.x, you will 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() >>> 

then

 >>> float(price_loyal) > 5000.0 False 

The Python version matters in this case, so it's probably worth mentioning which version it works with. Earlier ... with Python v 2.x

Your comparisons will be disabled before converting your string to float . For instance.

 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 be

 float(price_loyal) > 5000.0 False 

Other problems may occur, but it seems like one.

+6
source

I am not a Python encoder, but it looks like it complains that you are trying to compare a string with a float, and I think Python is not juggling for you.

You must convert the string to float, but this is done in Python.

+2
source

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


All Articles