-1

So I'm new to python and I'm making a tic tac toe game and for each players turn you pick a number from 0–8. It checks if it's an integer between 0–8, but I wanted to be able to take the response and replace the corresponding number in the list.

For instance, say my list looked like this

tiles = [0, 1, 2, 3, 4, 5, 6, 7, 8]

and for X's turn they typed 1.

I want it to replace 1 with an X on the board. I know I could do this with a whole bunch of if/elif/else statements but my friend did that and it looked super long and unnecessary.

I put it on GitHub in case you wanted to have look at the code to better understand what I'm talking about.

I'm using repl.it so I'm stuck on 3.8.2.

  • 1
    What tutorial are you reading that teaches lists but doesn't teach how to set list elements? – Kelly Bundy Feb 24 '22 at 16:24
  • @Tomerikoo Seems like overkill for this. – Kelly Bundy Feb 24 '22 at 16:57
  • @KellyBundy [Finding the index of an item in a list](https://stackoverflow.com/q/176918/6045800) – Tomerikoo Feb 24 '22 at 17:03
  • @Tomerikoo Still overkill. They already have the index. All they need to do is `tiles[x_turn] = 'X'`. – Kelly Bundy Feb 24 '22 at 17:04
  • 1
    [https://docs.python.org/3/tutorial/introduction.html#lists](https://docs.python.org/3/tutorial/introduction.html#lists) – wwii Feb 24 '22 at 17:07
  • @KellyBundy You're right I didn't realize how simple it was, every time I tried looking it up it never gave me a straight answer and looked complicated. If you leave an answer then I'll check it off as helpful. This was the most helpful and simple response yet. Thank you. – Anthony Periandri Feb 25 '22 at 16:02
  • @AnthonyPeriandri Meh, not interesting enough :-P. But feel free to write an answer with it yourself if you want ([that's ok](https://stackoverflow.com/help/self-answer)). Edit: Oh wait, the question is closed, we can't post answers anyway (at least I can't, maybe you as the question's author can, I'm not sure). – Kelly Bundy Feb 25 '22 at 16:17

3 Answers3

1

Something like this?

tiles = list(range(8+1))

while True:
    user_input = input("Enter a number: ")
    if not user_input.isdigit():
        continue
    number = int(user_input)
    if number not in tiles:
        continue
    tiles[number] = 'x'
    print(tiles)

Output:

Enter a number: asdasdasd
Enter a number: 3
[0, 1, 2, 'x', 4, 5, 6, 7, 8]
Enter a number: 4
[0, 1, 2, 'x', 'x', 5, 6, 7, 8]
Enter a number: 0
['x', 1, 2, 'x', 'x', 5, 6, 7, 8]
Enter a number: 0
Enter a number: 10
Enter a number: 

This isn't meant to be a complete example - you can only set crosses, not noughts. Also, the program will just keep asking you for input even if you've set all entries to crosses.

Paul M.
  • 10,481
  • 2
  • 9
  • 15
0

First of all you need to check whose turn is it (x or o) then use this

tiles = [0, 1, 2, 3, 4, 5, 6, 7, 8]
user_input = int(input())
if turn == 'x':
    tiles[tiles.index(user_input)] = 'x'

Of course you should fill in the necessary gaps

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Devil Ishere
  • 185
  • 1
  • 13
0

In addition to the other answers: move the board's construction inside of the function so that after you replace a tile the board can be reconstructed.

def board():

    play_board = str(tiles[0]) + "|" + str(tiles[1]) + "|" + str(tiles[2])
    play_board_1 = str(tiles[3]) + "|" + str(tiles[4]) + "|" + str(tiles[5])
    play_board_2 = str(tiles[6]) + "|" + str(tiles[7]) + "|" + str(tiles[8])

    print(play_board)
    print(divider)
    print(play_board_1)
    print(divider)
    print(play_board_2)
wwii
  • 23,232
  • 7
  • 37
  • 77