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]](https://i.gyazo.com/f0ea22cbd5b487a605ccbba29ad5943f.png)
![[Image: f0ea22cbd5b487a605ccbba29ad5943f.png]](https://i.gyazo.com/f0ea22cbd5b487a605ccbba29ad5943f.png)
![[Image: 29cab16e9bb2f16fb173f9b7440a20f7.png]](https://i.gyazo.com/29cab16e9bb2f16fb173f9b7440a20f7.png)
tic tac toe indentation help
|
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:
![]() ![]()
Mar-31-2018, 03:13 AM
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
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.
Mar-31-2018, 01:28 PM
(This post was last modified: Mar-31-2018, 01:29 PM by ilaytertman1.)
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)
Mar-31-2018, 01:35 PM
In this area:
if player1=="X": player2="O" else: player2="X"Indentation is off, should be 4 spaces.
Mar-31-2018, 01:38 PM
(This post was last modified: Mar-31-2018, 01:39 PM by ilaytertman1.)
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
Mar-31-2018, 01:44 PM
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]=player1has 8, not 4
Mar-31-2018, 02:12 PM
i fixed it but it keeps saying:
Quote:IndentationError: unindent does not match any outer indentation levelif 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.
Mar-31-2018, 08:52 PM
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.
|
|