Weird Loop Behaviour

Discussion in 'Python' started by Yigit Turgut, Jan 20, 2012.

  1. Yigit Turgut

    Yigit Turgut Guest

    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()
     
    Yigit Turgut, Jan 20, 2012
    #1
    1. Advertising

  2. Yigit Turgut

    MRAB Guest

    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.
     
    MRAB, Jan 20, 2012
    #2
    1. Advertising

  3. 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()
     
    Emile van Sebille, Jan 20, 2012
    #3
  4. On 20 January 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()


    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
     
    Arnaud Delobelle, Jan 20, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dorayme
    Replies:
    1
    Views:
    623
    richard
    Jan 21, 2011
  2. richard
    Replies:
    0
    Views:
    587
    richard
    Jan 21, 2011
  3. richard
    Replies:
    0
    Views:
    618
    richard
    Jan 21, 2011
  4. Mark Scott

    weird for loop behaviour

    Mark Scott, Jan 13, 2008, in forum: Javascript
    Replies:
    4
    Views:
    92
    Mark Scott
    Jan 14, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page