0

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?

4
  • 1
    Calling copy won't help you in any significant way. A copy of population[j] will contain new references to the same objects referenced by population[j], which you then assign directly into boardChild. You probably want deepcopy. Commented May 21, 2020 at 17:48
  • "all these posts mention the use of () or [] where they should not be used" yes, that's the same problem as in your code; the error message can only effectively mean one thing. The error occurs when you try to do boardChild[s] = boardy[s]; this tells you that either boardChild or boardy is a function. boardChild clearly has a local assignment that isn't a function, so it must be boardy, which was passed in. So you look at where the value came from, and discover that you have a typo. Commented May 21, 2020 at 17:49
  • "I also tried to find out if any of the used variables were known functions, but that does not seem to be true either." How did you try? Did you try, for example, print(type(boardy))? When you looked at the code where you called the function, did you try checking the value of childx and childy in between assigning them and calling the function? Commented May 21, 2020 at 17:50
  • "My compiler currently only gives an error about this line: " The TypeErrors don't occur at compile time, though. Commented May 21, 2020 at 17:53

2 Answers 2

2
childx = population[j].copy
childy = population[k].copy

If you are trying to use the copy method of a list, this will not do that. You are accessing the method, but not calling it so childx and childy are functions.

I admit I am not sure why the first loop doesn't raise this error though.

Sign up to request clarification or add additional context in comments.

1 Comment

"I admit I am not sure why the first loop doesn't raise this error though." Well, sometimes n will be 0. It might also be that on some runs, it did cause an error, and OP simply didn't notice that the error was happening there first on that run.
0

The problem here is that your variable boardy seems to be a variable. If you change that variable name to anything else it should work fine. Something like:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.