Python Forum
Tic Tac Toe except Valueerror help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tic Tac Toe except Valueerror help
#1
Hello,

Would anyone be able to help me with getting my Tic Tac Toe game to work pls?

I am trying to make it so that if the users input is invalid, eg, a letter or not in the range of 0-8,
it prints out "please enter a valid number!"

This is what i have so far:
def chooseMove (boardValues):   ###This is the players choice of where to go. (the player starts first always in this game.)
	validMove = False
	
	while (not validMove):
		printGameBoard(boardValues)
		squareChoice = int(input("Choose a square!")) -1 # takes away 1 from the chosen value by the player because the actual values of the board are 1 less than what they choose.
	
		if boardValues[squareChoice] != "X" and boardValues[squareChoice] != "O": ## checks to see that the chosen square is not already occupied.
			boardValues[squareChoice] = "X"
			validMove = True
		elif squareChoice not in range(0, 9):
			print("\nAnswer is not a number! please enter a valid number!")
			continue
		except ValueError:
            print("\nAnswer is not a number! please enter a valid number!")
               	continue
		else:
			print("Please choose a free square!")


	return checkForWin(boardValues)
so basically i do know that the elif square choice part is wrong but dont know how to fix it so if someone could help me would be appreciated.

thanks.
Reply


Messages In This Thread
Tic Tac Toe except Valueerror help - by Minindis02 - Jul-10-2018, 06:39 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020