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
#2
Rearrange part of your code like that (the rest is OK)

try:
    squareChoice = int(input("Choose a square!")) -1
except ValueError:
    print("\nAnswer is not a number! please enter a valid number!")
    continue

if boardValues[squareChoice] != "X" and boardValues[squareChoice] != "O"
may be re-written as
if boardValues[squareChoice] not in "OX":
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
(Jul-10-2018, 07:18 AM)volcano63 Wrote: Rearrange part of your code like that (the rest is OK)

try:
    squareChoice = int(input("Choose a square!")) -1
except ValueError:
    print("\nAnswer is not a number! please enter a valid number!")
    continue

if boardValues[squareChoice] != "X" and boardValues[squareChoice] != "O"
may be re-written as
if boardValues[squareChoice] not in "OX":
Hi, thanks for the reply,

By rearrange can you tell me if this is correct? Because it keeps giving me an error saying "Inconsistent Use of tabs and spaces in indentation".

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)
        try:
            squareChoice = int(input("Choose a square!")) -1
		except ValueError:
            print("\nAnswer is not a number! please enter a valid number!")
    	    continue
     
        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
        
        else:
            print("Please choose a free square!")
 
 
Reply
#4
(Jul-10-2018, 12:10 PM)Minindis02 Wrote: By rearrange can you tell me if this is correct? Because it keeps giving me an error saying "Inconsistent Use of tabs and spaces in indentation".

Python does not like mixture of tabs and spaces. I recommend to check you code for tabs - and replace them with 4 spaces. Adjust your editor to insert 4 spaces when you press tab
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
(Jul-10-2018, 01:24 PM)volcano63 Wrote:
(Jul-10-2018, 12:10 PM)Minindis02 Wrote: By rearrange can you tell me if this is correct? Because it keeps giving me an error saying "Inconsistent Use of tabs and spaces in indentation".

Python does not like mixture of tabs and spaces. I recommend to check you code for tabs - and replace them with 4 spaces. Adjust your editor to insert 4 spaces when you press tab

Hi,
The thing is it is set to 4 spaces and ive checked that theres no more than 4 spaces for the lines however it still says that it is inconsistent. Does anyone know why this could be...

thanks.
Reply
#6
Do a find and replace on tabs, replacing them all with four spaces.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(Jul-11-2018, 12:41 AM)Minindis02 Wrote:
(Jul-10-2018, 01:24 PM)volcano63 Wrote: Python does not like mixture of tabs and spaces. I recommend to check you code for tabs - and replace them with 4 spaces. Adjust your editor to insert 4 spaces when you press tab

Hi,
The thing is it is set to 4 spaces and ive checked that theres no more than 4 spaces for the lines however it still says that it is inconsistent. Does anyone know why this could be...

thanks.

Oh ok thanks that part is working now, do you or anyone know how to get the "in range0-9" part working? because i dont really know how to make it so that if someone inputs "20" or something it says "Please enter a valid number" or something.

THanks.
Reply
#8
(Jul-11-2018, 01:59 AM)Minindis02 Wrote:
(Jul-11-2018, 12:41 AM)Minindis02 Wrote:
(Jul-10-2018, 01:24 PM)volcano63 Wrote: Python does not like mixture of tabs and spaces. I recommend to check you code for tabs - and replace them with 4 spaces. Adjust your editor to insert 4 spaces when you press tab
Hi, The thing is it is set to 4 spaces and ive checked that theres no more than 4 spaces for the lines however it still says that it is inconsistent. Does anyone know why this could be... thanks.
Oh ok thanks that part is working now, do you or anyone know how to get the "in range0-9" part working? because i dont really know how to make it so that if someone inputs "20" or something it says "Please enter a valid number" or something. THanks.
says "Traceback (most recent call last):
File "C:\Users\Tissa\Desktop\TTTProgram.py", line 96, in <module>
won = chooseMove(boardList)
File "C:\Users\Tissa\Desktop\TTTProgram.py", line 21, in chooseMove
if boardValues[squareChoice] != "X" and boardValues[squareChoice] != "O": ## checks to see that the chosen square is not already occupied.
IndexError: list index out of range"
Reply


Forum Jump:

User Panel Messages

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