Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Please help! Python elif statement

So im trying create this really simple Tic Tac Toe game, however when i run this code it spits out a syntax error on line 45. Its saying the elif statement is invalid syntax?? I dont understand this what so ever. Help would be very apreciated!

board = [0, 1, 2,
         3, 4, 5,
         6, 7, 8]

import random
#Explain how game works
print("Welcome to a tic tac toe game. O always starts!")

#Print the board
def show():
    print(board[0],"|",board[1],"|",board[2])
    print("---------")
    print(board[3],"|",board[4],"|",board[5])
    print("---------")
    print(board[6],"|",board[7],"|",board[8])


def computer_choice():
    computer_pick = random.randint(0, 8)
    if board[computer_pick] != "x" and board[computer_pick] != "o":
        board[computer_pick] = "x"
    else:
        computer_choice()

#Make player choose to play O or X
O_or_X = input("Do you want to play as O or X? ")
if O_or_X.lower() == "o":
    print("Ok, your playing as {}".format(O_or_X))
elif O_or_X.lower() == "x":
    print("Ok, your playing as {}".format(O_or_X))
else:
    print("You can only input X or O!")
    play_as()

while True:
    show()
    if O_or_X == "o":
        try:
            human_pick = int(input("Where do you want to play? "))
        except ValueError:
            print("You can only input numbers!")
        if board[human_pick] != "x" and board[human_pick] != "o":
            board[human_pick] = "o"
    computer_choice()
    elif O_or_X == "x": # This is where it spits a syntax error!?
        computer_choice()
        try:
            human_pick = int(input("Where do you want to play? "))
        except ValueError:
            print("You can only input numbers!")
        if board[human_pick] != "x" and board[human_pick] != "o":
            board[human_pick] = "o"

1 Answer

It's because your elif block has to come directly after an if block, with nothing else in between. This is valid:

if some_condition:
    print("hi")
elif some_other_condition:
    print("hello")

This is not valid:

if some_condition:
    print("hi")
print("how are you?")  # This line either needs to be indented, or deleted.
elif some_other_condition:
    print("hello")

In your program, I think you simply need to indent line 44 so that it falls inside the if block.

That fixes the syntax error at least. You might want to do something about the fact that your program will run forever :)

ahhh got it, ty so much!