2012-05-28 28 views
6
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の部分が得られるまで実行されていることです。その後、それは停止し、秩序のないタイプについて文句を言う。私はそれが何を意味するのか完全に混乱しています。PythonでUnorderable Typeエラーの意味は何ですか?

+0

何について苦情を言いますか? – dukevin

+3

'price_loyal'は数値(4.75)と比較しようとしている文字列(' find'で見つかった数字が入っていても)ですか? 'float(price_royal)'しようとするとどうなりますか – Levon

+0

他の一般的なスクリプト言語とは異なり、Pythonは厳密に型付けされています。これは、文字列を数値に変換する場合は、明示的に行う必要があることを意味します。 –

答えて

6

price_loyalは数値(4.75)と比較しようとしている文字列(findで見つかった数字が含まれていても)ですか?あなたの比較のために

float(price_loyal)

UPDATE(感謝@agf)を試してください:PythonのV 3.xの

はあなたが言及したエラーメッセージが表示されます。

>>> float(price_loyal) > 5000.0 
False 

のPythonのバージョンに対し

>>> 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() 
>>> 

は、この場合の違いを作るので、いつもと協力してどのバージョン1に言及することはおそらく良いアイデア。 以前は... PythonのV 2.xのと

あなたの比較が最初floatにごstringを変換せずにオフになります。文字列とフロートとのこの比較はTrue

price_loyal > 5000.0 
True 
を与える

price_loyal 
'555.5' 

例えば、

フロートとフロートとのこの比較はFalseとして行うべきで

float(price_loyal) > 5000.0 
False 

、他の問題があるかもしれませんが、これを提供します1つのように見えます。

+3

これはPython 2の動作です。彼が得意な動作はPython 3です。 – agf

+0

4.75とpython_loyalの両方をfloat()を使って浮動小数点数に変換する必要がありました。> – Humility

+0

@Humility 7.75を 'float'に変換する必要はありません。すでに' price_loyal'という文字列であるためです。 – Levon

2

私はPythonコーダーはありませんが、文字列とフロートを比較しようとしていると不平を言っています。私はPythonがあなたのためにジャグリングしないと思います。

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

+0

'foo_float = float(foo_string)' –

+0

ありがとうございました、しかし、すべてのfloat didntはそれを修正しました。しかしBurnap氏は明示的とKDi私はちょうど無限ループの外でそれを取るつもりです – Humility

関連する問題