Page 114 -
P. 114
functions
What does the new program need to do?
The new program for Starbuzz needs to give the user two options.
The first option is to watch and wait for the price of coffee beans to drop.
If the user chooses this option, the program should run exactly as it did
before.
The second option is for the user to place an emergency order. If the user
chooses this option, the program should immediately display the current
price from the supplier’s website.
Here‛s the existing code for Starbuzz. You need to modify the program
to add an emergency report feature that will immediately report the
current price. Which parts of the code can you reuse to generate the
emergency report? Grab your pencil and circle the part of the code you
think you might reuse. Why do you think you‛ll need to resuse this code?
import urllib.request
import time
price = 99.99
while price > 4.74:
time.sleep(900)
page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html")
text = page.read().decode("utf8")
where = text.find('>$')
start_of_price = where + 2
end_of_price = start_of_price + 4
price = float(text[start_of_price:end_of_price])
print ("Buy!")
you are here 4 79