
62 第二章
各得其所
程式碼磁貼解答
增加新功能的程式碼散落在冰箱門上,你的任務是重新安排這些
磁貼,讓程式不斷循環,直到咖啡豆的價格降到 $4.74 或以下。
import urllib.request
while price > 4.74:
page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html")
text = page.read().decode("utf8")
where = text.find(‘>$’)
end_of_price = start_of_price + 4
start_of_price = where + 2
price = text[start_of_price:end_of_price]
print ("Buy!")
price = 99.99
是否記得
縮排這幾
行?它們
位在新增
的迴圈下
面。
這一行不應該縮排,
因為它位在迴圈以外。
ch02.indd 62 2011/8/6 上午 02:22:08