Jump to content


Photo

Chapter 2 While Loop


  • Please log in to reply
4 replies to this topic

#1 gonzo

gonzo

    New Member

  • Members
  • Pip
  • 5 posts

Posted 25 December 2014 - 07:45 PM

Instead of...

"price = 99.99
while price > 4.74:"

can I do...

if price < 4.74:
print("Buy!")

I don't understand the logic of using a while loop.

#2 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 27 December 2014 - 12:38 AM


Well, if you do that the price will be checked once and the program will exit.

As the server or the site keeps changing the value of price (you can verify that by clicking the link now and after some time); the while loop keeps pinging the server and updating the value of price till you get it lower than 4.74 or specified value.

When you get price lower than that it prints buy!

:)

Edited by #TM#, 27 December 2014 - 02:07 AM.


#3 gonzo

gonzo

    New Member

  • Members
  • Pip
  • 5 posts

Posted 28 December 2014 - 09:28 PM

Of course. Thank you. Didn't realize that.

#4 gonzo

gonzo

    New Member

  • Members
  • Pip
  • 5 posts

Posted 28 December 2014 - 10:43 PM

I just didn't get why they used the "price = 99.99
while price > 4.74:"
but think I do now. The loop keeps checking the price obviously because 99.99 is bigger than 4.74 and until it's not then you buy right away and "time.sleep(900)" is used so that the server isn't overloaded with requests. Right?


Well, if you do that the price will be checked once and the program will exit.

As the server or the site keeps changing the value of price (you can verify that by clicking the link now and after some time); the while loop keeps pinging the server and updating the value of price till you get it lower than 4.74 or specified value.

When you get price lower than that it prints buy!

:)



#5 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 30 December 2014 - 10:35 AM

I just didn't get why they used the "price = 99.99
while price > 4.74:"
but think I do now. The loop keeps checking the price obviously because 99.99 is bigger than 4.74 and until it's not then you buy right away and "time.sleep(900)" is used so that the server isn't overloaded with requests. Right?



You got it right. You can use small time interval of 10 or 20 sec. rather than 900.
Anyway, you got the idea. :) B)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users