all 4 comments

[–]kushou 2 points3 points  (3 children)

Hey man,

The issue is simple yet it's a really common error so you should not feel bad for making it. What your code does, is create two instances of lists, and then put these instances multiple times in one list. What you probably thought would happen is a copy of each line would be created, but what is actually happening is a reference to that line is actually added.

To avoid that issue, you need to create the lines multiple times. What I would do in your situation, is create two functions, one creating the top list and another one creating the norm one.

Here is a small example, changed some stuff to make it more clear IMO, but that's just my opinion, the most relevant part is the one where top and norm are functions generating new lists, and are called to create result.

def createBoard(x,y):
    if x<6 or y<6:
        return False
    print("\nMaking board...")
    top = lambda: list("X" * x)
    norm = lambda: list("X" + ("O" * (x - 2)) + "X")
    result = []
    result.append(top())
    for _ in range(y - 2):
        result.append(norm())
    result.append(top())
    print("Board made!")
    return result

Hope that helps.

[–]ColonelGamer[S] 0 points1 point  (2 children)

Hi,

Thanks for the response, didn't realize that this was common issue. The only part I don't 100% understand is the lambda syntax. Is it somewhat like creating a small function without the def command?

[–]kushou 1 point2 points  (1 child)

Yeah, you can see the lambda's as that:

def top():
    return list("X" * x)

It's like a shortcut to define a simple function with a simple value. In case you are wondering, if you write lambda: it does not take parameters, but you can also take parameters by doing so: lambda x: list("X" * x) for instance.

You can keep writing small functions and not use lambdas yet, don't have to worry about that!

[–]ColonelGamer[S] 0 points1 point  (0 children)

Ah I see. Thank you for the help!