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.")
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是它一直运行直到我得到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()
>>>
Run Code Online (Sandbox Code Playgroud)
而
>>> float(price_loyal) > 5000.0
False
Run Code Online (Sandbox Code Playgroud)
Python的版本在这种情况下有所不同,所以总是可以提到一个正在使用的版本.以前......使用Python v 2.x
您的比较将在不转换string为float第一个的情况下关闭.例如,
price_loyal
'555.5'
Run Code Online (Sandbox Code Playgroud)
这与string和float的比较给出了 True
price_loyal > 5000.0
True
Run Code Online (Sandbox Code Playgroud)
这与float和float的比较给出False了应有的效果
float(price_loyal) > 5000.0
False
Run Code Online (Sandbox Code Playgroud)
可能还有其他问题,但这看起来像是一个问题.