the script is failing without a useful report

MRAB python at mrabarnett.plus.com
Sun Sep 5 00:22:01 CEST 2010


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).



More information about the Python-list mailing list