0

May I know how do i edit a constant 2D array variable upon meeting certain conditions and then save into the variable for processing later ? Currently, with the code below, I was unable to store my data in userInput into the column variable.

When i print BOARD, the values are still unchanged ! How will i be able to traverse the 2D array as shown below and edit certain elements and save them back into the variable BOARD?

The source code is below :

for row in BOARD:

    if userInput not in row:
        userInput = raw_input ("Please enter a character into the program")
    if userInput in row:
        for column in row:
            if userInput != column:
                print column
                print userInput
                column = userInput
                break
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
misctp asdas
  • 973
  • 4
  • 13
  • 35
  • Related: [How to modify list entries during for loop?](http://stackoverflow.com/questions/4081217/how-to-modify-list-entries-during-for-loop) – Ashwini Chaudhary Oct 12 '14 at 14:56

1 Answers1

2

You can store them back with their corresponding indices, which are got with enumerate function, like this

for i, row in enumerate(BOARD):
    ...
    ...
    for j, column in enumerate(row):
        ...
        BOARD[i][j] = userInput
        ...

In this case, since you are altering the row, you can drop the outer enumerate, like Ashwini Chaudhary suggested in the comments

for row in BOARD:
    ...
    ...
    for i, column in enumerate(row):
        ...
        row[i] = userInput
        ...
Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497