0

I want my code to return a square grid full of "x"'s and a single "I" at location (3,2), as seen bellow:

xxxxx

xxxxx

xxixx

xxxxx

xxxxx

I wrote the code bellow originally

grid_frontend = [["x"] * 5] * 5


 for i in range(len(grid_frontend)):
        for j in range(len(grid_frontend[i])):
            if ((i == 3) and (j == 2)):
                grid_frontend[3][2] = 'i'
            print(grid_frontend[i][j], end='')
        print()

the end result of this was:

xxxxx

xxxxx

xxixx

xxixx

xxixx

I also attempted moving the "grid_frontend[3][2] = 'I'" outside of the for loops with out the if statement, this also returned a wrong grid:

xxixx

xxixx

xxixx

xxixx

xxixx

Im not sure why the grid being returned is always being printed incorrectly, there is no other location where the gird if edited, or the function this code is in is looped over.

  • 1
    You have committed one of the classic Python blunders. In your code snippet, there are exactly TWO lists. One contains 5 instances of the string "x". The other contains 5 references to the first list. If you change one, you change them all. – Tim Roberts Mar 09 '22 at 05:46

0 Answers0