Python,tictactoe,语法错误

时间:2016-08-09 16:09:55

标签: python python-3.x

我是Python新手。我发现这个网站TheHelloWorldProgram.com教我如何编写一个非常容易理解的tictactoe游戏,但我遇到了一些问题。我一直收到此语法错误消息"语法无效"它指向player = False。它突出了“玩家”#。为什么?我无法弄明白。谢谢您的帮助。下面你会看到游戏的代码,请原谅#comments。他们是为了我自己:

#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)]

1 个答案:

答案 0 :(得分:2)

1 = [' rock'等]应为t = 参考python(我认为大多数语言)不接受#作为变量名 你可以做t1,t_1,t,one,但不是1t,1_t。所以以数字开头的数字和变量。正如@Wickramaranga指出的那样。

并且@Jim指出关闭你的打印语句。检查python错误的好地方是错误指向的语句之前的语句。例如,

select * from   gpqueries:contracts.raw where    fiscal_year =2015

您的错误指向player = False,因为当python解释器执行

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

它没有看到结尾')'。