-1

I'm trying to create a basic game, and one of the stages of this game is a user input of a value from 1-9. If a user enters one of those values, the game works, but if they simply hit return/enter when they get to the field, I get a value error. Below is the error:

<ipython-input-6-061a946b3a03> in <module>
----> 1 ttt_game()

<ipython-input-5-b42c27ce7032> in ttt_game()
     36     while game_on == True:
     37         while choose_first == 0:
---> 38             player_guess = int(input('Choose a space 1-9: '))
     39             for position in board:
     40                 if position in board[player_guess] == ' ':

ValueError: invalid literal for int() with base 10: ''

I'm trying to figure out how to cause the input call to be either asked again, or to have the while loop restart and print a message like "Invalid entry, try again"

Here is the original code block if it helps.

 while game_on == True:
        while choose_first == 0:
            player_guess = int(input('Choose a space 1-9: '))
            for position in board:
                if position in board[player_guess] == ' ':
                    board[player_guess] = 'X'
                    display_board(board)
                    if win_check(board,'x') == False:
                        pass
                    else:
                        display_board(board)
                        print('Player 1 has won the game!')
                        break
                    
                elif board[player_guess] == 'O':
                    print ('Space already chosen, choose another.')
                    pass
                else:
                    choose_first = 1
                    pass

1 Answers1

0

You can wrap the input around a while loop to test if the value is numeric or digit before converting it to an integer.

while game_on == True:
while choose_first == 0:

    result = ""
    while(not result.isdigit()): 
        # you can also use result.isnumeric()
        result = input('Choose a space 1-9: ')

    player_guess = int(result)
    for position in board:
        if position in board[player_guess] == ' ':
            board[player_guess] = 'X'
            display_board(board)
            if win_check(board, 'x') == False:
                pass
            else:
                display_board(board)
                print('Player 1 has won the game!')
                break

        elif board[player_guess] == 'O':
            print('Space already chosen, choose another.')
            pass
        else:
            choose_first = 1
            pass
Ibukun Muyide
  • 1,294
  • 1
  • 15
  • 23
  • Unfortunately I can't get this solution to work either. When applied directly as shown in the example, python throws an error the second it hits the while loop without even asking for the player's input. When I tried fixing the indentation of the rest of the for loops, etc, and adding a break for that single while loop, python throws an unboundlocalerror because player_guess is referenced before it's assignment.. – Brandon Covington Jul 09 '20 at 00:57
  • @BrandonCovington what error are you getting , I have removed the condition for `> 0 and < 9` for now – Ibukun Muyide Jul 09 '20 at 01:03
  • the error was as a result of the `> 0 and <9` I initially added , because as at then the value of the result string is empty , you can remove that – Ibukun Muyide Jul 09 '20 at 01:05
  • This ended up working, I appreciate the help! – Brandon Covington Jul 10 '20 at 16:56