-1

Im just learning Python now and trying to do the tic tac toe game by myself. I have written this code , but I do not know what's wrong with my logic.

def player_choice(board):  #this function asks for the players next position
    i= 0
    if full_board_check(board):
        print ("All the positions have been filled, the game is over")
        
    while i not in [1,2,3,4,5,6,7,8,9] or board[i]=='X' or board[i]=='Y':  
         i=input('Please choose a number')
    return i       

As opposed to this one , which is deemed correct:

def player_choice(board):    
    position = 0    
    while poistion not in [1,2,3,4,5,6,7,8,9] or not space_check(board,position):
        position=int(input("Please choose a valid position"))
    return position
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Please try to think carefully about the logic of the code. What happens when the board is full, such that `full_board_check(board)` is `True`? It will print the message, right? And **then what** - will it continue with the rest of the function? **Should** it? Do you know a way to prevent that from happening? If the board is full, **what should the result from the function be**? – Karl Knechtel Mar 15 '23 at 23:39
  • There are many other things that could possibly be wrong here, but we do not have a proper [mre] and therefore cannot give a proper answer - aside from noting things that are definitely wrong, such as expecting the `i=input('Please choose a number')` result to be possibly `in [1,2,3,4,5,6,7,8,9]`. But you already have correct code shown to you for dealing with that, so there is no clear question there, either. – Karl Knechtel Mar 15 '23 at 23:41
  • Please keep in mind that Stack Overflow does not provide a [debugging](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) service. – Karl Knechtel Mar 15 '23 at 23:42

1 Answers1

-1

input returns a string. In the second code you are converting to integer, so it is ok.

fernand0
  • 310
  • 1
  • 10