the script is failing without a useful report

Discussion in 'Python' started by nvictor, Sep 4, 2010.

  1. nvictor

    nvictor Guest

    hi all,

    need your help. i get a traceback that doesn't tell much about the
    actual error in my code:

    Traceback (most recent call last):
    File ".\eightqueens.py", line 32, in <module>
    generate(n, x, col, up, down)
    File ".\eightqueens.py", line 17, in generate
    else: generate(n, x, col, up, down)
    File ".\eightqueens.py", line 17, in generate
    else: generate(n, x, col, up, down)
    File ".\eightqueens.py", line 17, in generate
    else: generate(n, x, col, up, down)
    File ".\eightqueens.py", line 17, in generate
    else: generate(n, x, col, up, down)
    File ".\eightqueens.py", line 17, in generate
    else: generate(n, x, col, up, down)
    File ".\eightqueens.py", line 17, in generate
    else: generate(n, x, col, up, down)
    File ".\eightqueens.py", line 17, in generate
    else: generate(n, x, col, up, down)
    File ".\eightqueens.py", line 17, in generate
    else: generate(n, x, col, up, down)
    File ".\eightqueens.py", line 17, in generate
    else: generate(n, x, col, up, down)
    File ".\eightqueens.py", line 17, in generate
    else: generate(n, x, col, up, down)

    here is the full source:

    def generate(n, x, col, up, down):
    for h in range(8):
    # place a queen
    if col[h] and up[n-h] and down[n+h]:
    x[n] = h
    col[h] = False
    up[n-h] = False
    down[n+h] = False
    n += 1

    # 8 queen placed?
    if n == 8: print x
    else: generate(n, x, col, up, down)

    # displace a queen
    n -= 1
    col[h] = True
    up[n-h] = True
    down[n+h] = True


    if __name__ == '__main__':
    n = 0
    x = [None]*8
    col = [True]*8
    up = [True]*15
    down = [True]*15
    generate(n, x, col, up, down)


    thanks a lot in advance
     
    nvictor, Sep 4, 2010
    #1
    1. Advertising

  2. nvictor

    MRAB Guest

    On 04/09/2010 22:22, nvictor wrote:
    > hi all,
    >
    > need your help. i get a traceback that doesn't tell much about the
    > actual error in my code:
    >
    > Traceback (most recent call last):
    > File ".\eightqueens.py", line 32, in<module>
    > generate(n, x, col, up, down)
    > File ".\eightqueens.py", line 17, in generate
    > else: generate(n, x, col, up, down)
    > File ".\eightqueens.py", line 17, in generate
    > else: generate(n, x, col, up, down)
    > File ".\eightqueens.py", line 17, in generate
    > else: generate(n, x, col, up, down)
    > File ".\eightqueens.py", line 17, in generate
    > else: generate(n, x, col, up, down)
    > File ".\eightqueens.py", line 17, in generate
    > else: generate(n, x, col, up, down)
    > File ".\eightqueens.py", line 17, in generate
    > else: generate(n, x, col, up, down)
    > File ".\eightqueens.py", line 17, in generate
    > else: generate(n, x, col, up, down)
    > File ".\eightqueens.py", line 17, in generate
    > else: generate(n, x, col, up, down)
    > File ".\eightqueens.py", line 17, in generate
    > else: generate(n, x, col, up, down)
    > File ".\eightqueens.py", line 17, in generate
    > else: generate(n, x, col, up, down)
    >
    > here is the full source:
    >
    > def generate(n, x, col, up, down):
    > for h in range(8):
    > # place a queen
    > if col[h] and up[n-h] and down[n+h]:
    > x[n] = h
    > col[h] = False
    > up[n-h] = False
    > down[n+h] = False
    > n += 1
    >
    > # 8 queen placed?
    > if n == 8: print x
    > else: generate(n, x, col, up, down)
    >
    > # displace a queen
    > n -= 1
    > col[h] = True
    > up[n-h] = True
    > down[n+h] = True
    >
    >
    > if __name__ == '__main__':
    > n = 0
    > x = [None]*8
    > col = [True]*8
    > up = [True]*15
    > down = [True]*15
    > generate(n, x, col, up, down)
    >

    The traceback ends with:

    RuntimeError: maximum recursion depth exceeded

    I think what's happening is that if the 'if' condition just after the
    "# place a queen" comment is false then 'n' won't be incremented, and
    therefore 'generate' will call itself with the exactly same values
    which were passed in, resulting in infinite recursion (or at least
    until the limit is reached).
     
    MRAB, Sep 4, 2010
    #2
    1. Advertising

  3. nvictor

    nvictor Guest

    thank you so much.
     
    nvictor, Sep 4, 2010
    #3
    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. Toby Inkster

    Useful script perhaps

    Toby Inkster, Aug 27, 2005, in forum: HTML
    Replies:
    0
    Views:
    368
    Toby Inkster
    Aug 27, 2005
  2. Ben
    Replies:
    1
    Views:
    1,611
    Alexey Smirnov
    Jun 11, 2007
  3. Ryan Liu
    Replies:
    1
    Views:
    651
    Alexey Smirnov
    Mar 17, 2009
  4. EdwardATeller
    Replies:
    2
    Views:
    148
    J├╝rgen Exner
    Sep 6, 2008
  5. Replies:
    3
    Views:
    268
    Adam G
    Oct 15, 2012
Loading...

Share This Page