Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Weird Loop Behaviour

Reply
Thread Tools

Weird Loop Behaviour

 
 
Yigit Turgut
Guest
Posts: n/a
 
      01-20-2012
Hi,

In the following code, I am trying to run "black" screen for 3 seconds
and respectively 2 seconds "white" screen. Black routine takes 3
seconds and white 2 seconds, 2 x black + white = 8 seconds which
should be the expected value but when I run it I get black-white-black-
white instead of black-white-black. Couldn't figure out what is
wrong thus sharing the code as well ;

white = False
while(end<8.00):
end = time.time() - start
if white:
screen.fill((255, 255, 255))
time.sleep(2)
else:
screen.fill((0, 0, 0))
time.sleep(3)
white = not white
pygame.display.update()
pygame.quit()
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      01-20-2012
On 20/01/2012 20:47, Yigit Turgut wrote:
> Hi,
>
> In the following code, I am trying to run "black" screen for 3 seconds
> and respectively 2 seconds "white" screen. Black routine takes 3
> seconds and white 2 seconds, 2 x black + white = 8 seconds which
> should be the expected value but when I run it I get black-white-black-
> white instead of black-white-black. Couldn't figure out what is
> wrong thus sharing the code as well ;
>
> white = False
> while(end<8.00):
> end = time.time() - start
> if white:
> screen.fill((255, 255, 255))
> time.sleep(2)
> else:
> screen.fill((0, 0, 0))
> time.sleep(3)
> white = not white
> pygame.display.update()
> pygame.quit()


Could it be because you're setting 'end' after testing it?

It might be simpler as:

while time.time() - start < 8:

Also, should it really be sleeping before updating the display? I
would've thought that it should be sleeping _after_ updating the
display.
 
Reply With Quote
 
 
 
 
Emile van Sebille
Guest
Posts: n/a
 
      01-20-2012
On 1/20/2012 12:47 PM Yigit Turgut said...
> Hi,
>
> In the following code, I am trying to run "black" screen for 3 seconds
> and respectively 2 seconds "white" screen. Black routine takes 3
> seconds and white 2 seconds, 2 x black + white = 8 seconds which
> should be the expected value but when I run it I get black-white-black-
> white instead of black-white-black. Couldn't figure out what is
> wrong thus sharing the code as well ;
>
> white = False
> while(end<8.00):
> end = time.time() - start

you're setting end's value before the display happens, so while tests
the values 0,3,5 before getting after the fourth pass 8. Move this
after to white = note white and I suspect you'll be OK.

HTH,

Emile




> if white:
> screen.fill((255, 255, 255))
> time.sleep(2)
> else:
> screen.fill((0, 0, 0))
> time.sleep(3)
> white = not white
> pygame.display.update()
> pygame.quit()



 
Reply With Quote
 
Arnaud Delobelle
Guest
Posts: n/a
 
      01-20-2012
On 20 January 2012 20:47, Yigit Turgut <(E-Mail Removed)> wrote:
> Hi,
>
> In the following code, I am trying to run "black" screen for 3 seconds
> and respectively 2 seconds "white" screen. Black routine takes 3
> seconds and white 2 seconds, 2 x black + white = 8 seconds which
> should be the expected value but when I run it I get black-white-black-
> white * instead of black-white-black. Couldn't figure out what is
> wrong thus sharing the code as well ;
>
> white = False
> * * while(end<8.00):
> * * *end = time.time() - start
> * * *if white:
> * * * screen.fill((255, 255, 255))
> * * * *time.sleep(2)
> * * *else:
> * * * *screen.fill((0, 0, 0))
> * * * *time.sleep(3)
> * * *white = not white
> * * *pygame.display.update()
> * * pygame.quit()


This is cryptic. You'd be better off with something like

black = 0, 0, 0
white = 255, 255, 255
for color, wait in (black, 3), (white, 2), (black, 3):
screen.fill(color)
pygame.display.update()
time.sleep(wait)
pygame.quit()

--
Arnaud
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
weird for loop behaviour Mark Scott Javascript 4 01-14-2008 07:58 PM
debugger behaviour different to execution behaviour Andy Chambers Java 1 05-14-2007 09:51 AM
Weird behaviour of SetAuthCookie, bug? Strange Cat ASP .Net 1 03-01-2004 04:10 PM
FormsAuthentication - Weird Behaviour dotCore ASP .Net 1 02-09-2004 03:52 AM



Advertisments