I have this error when executing my code. I looked around on other posts about this, but all these posts mention the use of () or [] where they should not be used. However, in my case I do not see that being the issue as the only thing I am trying to do is overwrite the value of an index of one list with another item of another list. This is the code I am trying to call it in:
def reproduce(boardx, boardy, nqueens):
boardChild = [-1] * nqueens
n = random.randint(0, nqueens - 1) #percentage of how much of one parent is reproduced and how much of the other parent
d = 0
for d in range(n): # the first n part of parent x
boardChild[d] = boardx[d]
s = d + 1
for s in range(nqueens - 1): # the last n-(len(board)-1) part of parent y
boardChild[s] = boardy[s]
return boardChild
Python currently only gives an error about this line:
boardChild[s] = boardy[s]
but not the similar line in the loop above it. This is how I call the function (population[j] and population[k] are lists for reference):
childx = population[j].copy
childy = population[k].copy
child = reproduce(childx, childy, nqueens)
I also tried to find out if any of the used variables were known functions, but that does not seem to be true either. I am completely lost with this one. Is anyone able to help?
copywon't help you in any significant way. A copy ofpopulation[j]will contain new references to the same objects referenced bypopulation[j], which you then assign directly intoboardChild. You probably wantdeepcopy.boardChild[s] = boardy[s]; this tells you that eitherboardChildorboardyis a function.boardChildclearly has a local assignment that isn't a function, so it must beboardy, which was passed in. So you look at where the value came from, and discover that you have a typo.print(type(boardy))? When you looked at the code where you called the function, did you try checking the value ofchildxandchildyin between assigning them and calling the function?TypeErrors don't occur at compile time, though.