Python Forum
tic tac toe indentation help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tic tac toe indentation help
#1
i crated a simple tic tac toe game in python and it keeps telling me i have an indentation problem i will appreciate if someone would be able to help me. here's the code of the game:
[Image: f0ea22cbd5b487a605ccbba29ad5943f.png]
[Image: 29cab16e9bb2f16fb173f9b7440a20f7.png]
Reply
#2
Indentation problems can be caused by mixing spaces and tabs because a tab is a different number of spaces in the IDE than from the command line for example. Check that the program uses spaces only for indentation
Reply
#3
After the winner function definition, there is a piece of code with a different indentation level. One space I as I see it.

Next time post the code between code tags. A picture cannot be tested if we want to.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
here's the code properly
def printboard(a,b,c):
	print(a)
	print(b)
	print(c)

def draw(*biglist):
	draww=true
	for i in biglist:
		if i=="":
			draww=false
		else:
			pass
		 	
    return draww
    def winner(a,b,c):
	iswinner=false
	if a[0]=="X" and a[1]=="X" and a[2]=="X":
		iswinner=true
	elif b[0]=="X" and b[1]=="X" and b[2]=="X":
		iswinner=true	
    elif c[0]=="X" and c[1]=="X" and c[2]=="X":
		iswinner=true	
    elif a[0]=="X" and b[0]=="X" and c[0]=="X":
		iswinner=true
	elif a[1]=="X" and b[1]=="X" and c[1]=="X":
		iswinner=true	
	elif a[2]=="X" and b[2]=="X" and c[2]=="X":
		iswinner=true
	elif a[0]=="X" and b[1]=="X" and c[2]=="X":
		iswinner=true
	elif a[2]=="X" and b[1]=="X" and c[0]=="X":
		iswinner=true
	elif a[0]=="O" and a[1]=="O" and a[2]=="O":
		iswinner=true
	elif b[0]=="O" and b[1]=="O" and b[2]=="O":
		iswinner=true	
    elif c[0]=="O" and c[1]=="O" and c[2]=="O":
		iswinner=true	
    elif a[0]=="O" and b[0]=="O" and c[0]=="O":
		iswinner=true
	elif a[1]=="O" and b[1]=="O" and c[1]=="O":
		iswinner=true	
	elif a[2]=="O" and b[2]=="O" and c[2]=="O":
		iswinner=true
	elif a[0]=="O" and b[1]=="O" and c[2]=="O":
		iswinner=true
	elif a[2]=="O" and b[1]=="O" and c[0]=="O":
		iswinner=true											
    return iswinner
    
gameon=true
player1=input("do you want to be X or O")
turn=1
if player1=="X":
 player2="O"	
else:
 player2="X"


list1=["","",""]
list2=["","",""]
list3=["","",""]
print("enjoy the game")
printboard(list1,list2,list3)

while gameon:
	if turn%2!=0:
		player="player1"
		answer=input("chose the location you want to insert player1 (1-9)")
	else:
	    player="player2"
	    answer=input("chose the location you want to insert player2 (1-9)")
     

    if player=="player1":
 		if answer>=1 and answer<=3:		
 		list1[answer-1]=player1
   	    elif answer>=4 and answer<=6:		
 		list2[answer-4]=player1
    	else:  
   	    list3[answer-7]=player1
 
    else:
		if answer>=1 and answer<=3:		
 		list1[answer-1]=player1
    	elif answer>=4 and answer<=6:		
 		list2[answer-4]=player2
   	    else:  
        list3[answer-7]=player3

    if draw(list1,list2,list3):
    	print("its a draw")
    	gameon=false

    if winner(list1,list2,list3):
    	if turn%2==0:
    		print("the winner is player 2")
    		gameon=false
    	else:
    		print("the winner is player 1")
    		gameon=false
    		

can you please let me know in witch line is the indentation problem (i know its a few)
Reply
#5
In this area:
if player1=="X":
 player2="O"    
else:
 player2="X"
Indentation is off, should be 4 spaces.
Reply
#6
ty but it keeps telling me that in this part that's an indentation problem either:
 if player=="player1":
        if answer>=1 and answer<=3:       
        list1[answer-1]=player1
        elif answer>=4 and answer<=6:     
        list2[answer-4]=player1
        else:  
        list3[answer-7]=player1
  
    else:
        if answer>=1 and answer<=3:       
        list1[answer-1]=player1
        elif answer>=4 and answer<=6:     
        list2[answer-4]=player2
        else:  
        list3[answer-7]=player3
 
    if draw(list1,list2,list3):
        print("its a draw")
        gameon=false
 
    if winner(list1,list2,list3):
        if turn%2==0:
            print("the winner is player 2")
            gameon=false
        else:
            print("the winner is player 1")
            gameon=false
             
Reply
#7
4 spaces per indentation level,
if player=="player1":
       if answer>=1 and answer<=3:       
       list1[answer-1]=player1
       elif answer>=4 and answer<=6:     
       list2[answer-4]=player1
       else:  
       list3[answer-7]=player1
has 8, not 4
Reply
#8
i fixed it but it keeps saying:
Error:
File "<tokenize>", line 75 if player=="player1": ^ IndentationError: unindent does not match any outer indentation level
Reply
#9
Quote:IndentationError: unindent does not match any outer indentation level
if player=="player1": is at a different indentation level than if turn%2!=0: (outer indentation level) Again, it could be mixing tabs and spaces that causes this. Most editors have a "replace tabs with spaces" option.
Reply
#10
this is not rocket science, start at line one and look at each line, make sure indentation for that line is correct and continue to end.
Reply


Forum Jump:

User Panel Messages

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