7
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 の部分を取得するまで実行されることです。その後、停止し、順序付けできない型について不平を言います。それが何を意味するのか、私は完全に混乱しています。

4

2 に答える 2

6

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.

于 2012-05-28T04:03:18.293 に答える
2

私は Python コーダーではありませんが、文字列を float と比較しようとしていることに不満を感じているようで、Python はあなたに代わってジャグリングしないと思います。

文字列を float に変換する必要がありますが、これは Python で行われます。

于 2012-05-28T04:02:30.863 に答える