O'Reilly Forums: Chapter 2 - While Loop - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 2 - While Loop

#1 User is offline   Nathan King 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 06-February 12

Posted 06 February 2012 - 01:54 AM

While working through long exercise at the end of chapter 2, I noticed that due to the positioning of the time.sleep(900) - the program waits 15 minutes before attempting to retrieve the price.

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!")


Shouldn't time.sleep(900) appear at the end of the loop so the program immediately retrieves a price to check if another iteration of the loop is actually needed?

import urllib.request
import time

price = 99.99
while price > 4.74:
    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])
    time.sleep(900)
print ("Buy!")


Just wondering about the positioning of the .sleep delay.

This post has been edited by Nathan King: 06 February 2012 - 01:55 AM

0

#2 User is offline   xxViennaxx 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 26-February 12

Posted 26 February 2012 - 03:42 PM

Your code is just more effective than the one in the book. Good job!!!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users