English 中文(简体)
无序类型错误在 Python 中意味着什么?
原标题: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) (价格_ loyal)

<强力> UPDATE (感谢@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 < strong> v 2. x 一起 。

您的比较将会关闭, 而不首先将您的 < code> string 转换为 < code> float 。 例如,

price_loyal
 555.5 

与字符串和浮控的比较 True

price_loyal > 5000.0
True

与浮和浮给出 False 的比较

float(price_loyal) > 5000.0
False

可能还有其他问题, 但看起来像一个问题。

问题回答

我不是 Python 编码器, 但它看起来像是抱怨 你试图比较一个字符串 和一个浮标, 我猜Python 不杂耍你。

您应该将字符串转换为浮点, 但是在 Python 完成的 。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签