I’m trying to learn python. I am using 3.1.2 and the o’reilly book is using 3.0.1
here is my code:
import urllib.request
price = (99.99)
while price > 4.74:
page = urllib.request.urlopen ("http://www.beans-r-us.biz/prices-loyalty.html")
text = page.read().decode("utf8")
where = text.find('>$')
start_of_price = where + 2
end_of_price = start_of_price + 6
price = float(text[start_of_price:end_of_price])
print ("Buy!")
Here is my error:
Traceback (most recent call last):
File "/Users/odin/Desktop/Coffe.py", line 14, in <module>
price = float(text[start_of_price:end_of_price])
ValueError: invalid literal for float(): 4.59</
>>>
What is wrong?
You are calculating
end_of_priceasstart_of_price + 6. Actually your price value seems to be only 4 characters long, so that you also include the two characters following in the string you want to convert to afloat. Python then complains that4.59</is not a number.If you instead set
end_of_pricetostart_of_price + 4it should work.