Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19166 > unrolled thread

Weird Loop Behaviour

Started byYigit Turgut <y.turgut@gmail.com>
First post2012-01-20 12:47 -0800
Last post2012-01-20 23:10 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Weird Loop Behaviour Yigit Turgut <y.turgut@gmail.com> - 2012-01-20 12:47 -0800
    Re: Weird Loop Behaviour MRAB <python@mrabarnett.plus.com> - 2012-01-20 21:10 +0000
    Re: Weird Loop Behaviour Emile van Sebille <emile@fenx.com> - 2012-01-20 13:20 -0800
    Re: Weird Loop Behaviour Arnaud Delobelle <arnodel@gmail.com> - 2012-01-20 23:10 +0000

#19166 — Weird Loop Behaviour

FromYigit Turgut <y.turgut@gmail.com>
Date2012-01-20 12:47 -0800
SubjectWeird Loop Behaviour
Message-ID<23de2ba8-dccc-404c-92f3-da0023c12646@t2g2000yqk.googlegroups.com>
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()

[toc] | [next] | [standalone]


#19167

FromMRAB <python@mrabarnett.plus.com>
Date2012-01-20 21:10 +0000
Message-ID<mailman.4895.1327093827.27778.python-list@python.org>
In reply to#19166
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.

[toc] | [prev] | [next] | [standalone]


#19170

FromEmile van Sebille <emile@fenx.com>
Date2012-01-20 13:20 -0800
Message-ID<mailman.4898.1327094440.27778.python-list@python.org>
In reply to#19166
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()

[toc] | [prev] | [next] | [standalone]


#19173

FromArnaud Delobelle <arnodel@gmail.com>
Date2012-01-20 23:10 +0000
Message-ID<mailman.4901.1327101047.27778.python-list@python.org>
In reply to#19166
On 20 January 2012 20:47, Yigit Turgut <y.turgut@gmail.com> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web