Vuken Vuken - 3 months ago 11
Python Question

Python, tictactoe, syntax error

I am new with Python. I found this website TheHelloWorldProgram.com that teaching me how to write a tictactoe game that very easy to understand but I ran into some problem. I keep getting this syntax error message "invalid syntax" and it pointed to player = False. it highlighted 'player'. Why? I couldn't figured it out. Thanks for the help. Below you will see the code of the game, please excuse the #comments. They are for myself:

#importing randint from the random module
from random import randint

#create a list of play options
1 = ['Rock', 'Paper', 'Scissors']

#assign a random play to the computer
computer = t[randint(0,2)]

#set player to False
player = False

while player == False:
#set player to True
player = input("Rock, Paper, Scissors?")

#if player = computer, it is a Tie!
if player == computer:
print("Tie!")

#else if player = ROCK
elif player == "Rock":
#computer = paper
if computer == "Paper":
#player lost, paper beats rock
print("You lose!", computer, "covers", player)
else:
#player win, rock beats scissors
print("You win!", player, "smashes", computer)

#else if player = PAPER
elif player == "Paper":
#if computer = scissors
if computer == "Scissors":
#player lost, scissors beats paper
print("You lose!", computer, "cut", player)
else:
#player win, paper beats rock
print("You win!", player, "covers", computer)

#else if player = SCISSORS
elif player == "Scissors":
if computer == "Rock":
print("You lose!", computer, "smashes", player)
else:
print("You win!", player, "cut", computer)
else:
print("That's not a valid play. Check your spelling!"

#player was set to True, but we want it to be False so the loop continues
player = False
computer = t[randint(0,2)]

Answer

The 1 = ['rock',etc] should be t = for reference python (and I think most languages) don't accept # as variable names you could do t1, t_1, t, one, but not 1t, 1_t,. So numbers and variables that start with numbers. as @Wickramaranga pointed out.

and as @Jim pointed out close your print statements. A good place to check with python errors is statements before the statement the error points to. So for example,

print("That's not a valid play.  Check your spelling!"

#player was set to True, but we want it to be False so the loop continues
player = False

Your error points to player = False, because when the python interpreter was executing the

print("That's not a valid play.  Check your spelling!"

It didn't see the ending ')'.

Comments