[英]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.")
我遇到的问题是它一直运行直到我得到4.74部分。 然后它停止并抱怨一种不可理解的类型。 我对这意味着什么感到困惑。
price_loyal
是一个字符串(即使它包含你在find
的数字),你试图与数值(4.75)进行比较? 为了比较试试
float(price_loyal)
更新 (谢谢@agf):
使用Python v 3.x,您会收到您提到的错误消息。
>>> 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()
>>>
而
>>> float(price_loyal) > 5000.0
False
Python的版本在这种情况下有所不同,因此总是可以提到一个正在使用的版本。 以前......使用Python v 2.x
您的比较将在不首先将string
转换为float
情况下关闭。 例如,
price_loyal
'555.5'
这与string和float的比较给出了True
price_loyal > 5000.0
True
与float和float的这种比较给出了False
float(price_loyal) > 5000.0
False
可能还有其他问题,但这看起来像是一个问题。
我不是Python编码器,但看起来它正在抱怨你试图将字符串与浮点数进行比较,我猜Python不会为你而烦恼。
您应该将字符串转换为float,但这是在Python中完成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.