Home > Net >  how to save a value inside a loop and add on it variables?
how to save a value inside a loop and add on it variables?

Time:11-06

I will be more specific: what I ask is how can I have a loop that saves its output every iteration and thus on every next iteration the loop does adds a value on the existing output, then it saves this too and repeat. for me, this is relevant for my tic tac toe I am building, which consist of a string that I desire to add on it variables (in this case a player who is either "x" or "o") afterwards, it needs to be saved. when additing another variable it should be shown in the same "board" that contains the previous variable. the problem here is, that I don't know how to do such saving action and then add on the SAME string another variable.

   def board(player, position):
     def saving():
    
      for i in range(1,9): 
       if position == i:
          posi = ["nothing","-","-","-","-","-","-","-","-","-"]
          posi[i] = player
          set1 = f"{posi[1]} | {posi[2]} | {posi[3]}\n"
          set2 = f"{posi[4]} | {posi[5]} | {posi[6]}\n"
          set3 = f"{posi[7]} | {posi[8]} | {posi[9]}\n"
          return set1, set2, set3
          continue 
     print1,_,_=saving()
     _,print2,_=saving()
     _,_,print3=saving()
     print(print1)
     print(print2)
     print(print3)

actually I am unfamiliar with what specific function can do a saving action. thus I had tried to play with list because I noticed that it appears in scripts that have such action. I tried to add on "continue" to see wether that gives the saving action. both didn't work. I will add that for clarity, regarding the given script, the "position" is an int that can only be in the range of 1-9, while "player" is a string that is x or o.

CodePudding user response:

To make updates upon the same grid, you'll want to save that grid as a variable outside your function and make updates to the grid using the board (record_move below) function.

Additionally, I encourage you to make use of nested lists to represent your grid. Given a nested list, we can access the upper-right corner with game_grid[0][0], the middle as game_grid[1][1], and so on. I know you had indices 1-9 but it is customary in most programming languages (including python) to begin counting at zero. I'm going to use 0-indexing from this point forward.

And, also, I think we should rename some of your variables and function for clarity. I'll call the function that records moves record_move instead of board. I'll call the 'x' or the 'o' that a player chooses move instead of player. And, lastly, I'll call the position position just like before.

So your script would look something like:


def record_move(move, position, game_grid):
    # implementation here


game_grid = [["-","-","-"], ["-","-","-"], ["-","-","-"]]

# Now, here, you can do something with game_grid. 
# Perhaps you want to prompt the user for input within a while loop.
# That's a relatively common procedure that you can search for elsewhere.
# But the main point is that now you can use the function board(...) to 
# make updates to the same game_grid.


Ok, onto the implementation of record_move itself. You only need one function, not two.

For the first two lines of your function, recall that you can set a location i,j of a nested list with game_grid[I][j] = VALUE. We can convert from position to two coordinates by doing something like

i = position // 3 # "a // b" gives us the floor of "a/b", so for example 4 // 3 = 1
j = position % 3 # "a % b" gives us the remainder of "a/b" so for example 4 % 3 is 1

Test it yourself and you will see that this lets you convert all 9 positions (numbered 0-8) to coordinates.

For the third line of your function, you simply need to set game_grid at coordinate i,j to move.

In the fourth line just return game_grid.

That should set you up to solve your issue. If you really want to stick to 1-indexing, just subtract 1 from position before doing the calculations in your function's lines 1 and 2.

Lastly, this task would be well-suited by object oriented programming. If you have any experience with that, I suggest instead representing the entire grid as a class and making the record_move function a member of that class.

  • Related