Python 如何打印井字游戏板?

Python 如何打印井字游戏板?,python,python-3.x,tic-tac-toe,Python,Python 3.x,Tic Tac Toe,我正在试着打印Tic Tac Toe游戏的棋盘。当我尝试运行它时,什么都没有发生,它说有无效的语法。无效的语法表示这是我的printboard函数中的一部分 我看不出我的代码有什么问题 我怎样才能让它打印电路板 #Tic-Tac-Toe Game import os import time import random board = [" " for x in range(10)] def printTitle(): print""" ----------------

我正在试着打印Tic Tac Toe游戏的棋盘。当我尝试运行它时,什么都没有发生,它说有无效的语法。无效的语法表示这是我的printboard函数中的一部分

我看不出我的代码有什么问题

我怎样才能让它打印电路板

#Tic-Tac-Toe Game

import os
import time
import random


board = [" " for x in range(10)]

def printTitle():
    print"""

----------------     1 | 2 | 3
TIC - TAC - TOE      4 | 5 | 6
________________     7 | 8 | 9

TO PLAY TIC - TAC - TOE, YOU NEED TO GET THREE IN A ROW.
YOUR CHOICES ARE BETWEEN 1 TO 9.

"""

def printBoard():
    print ( "   |   |   ")
    print (" "+board[1]+" | "+board[2]+" | "+board[3]+"  ")
    print ("   |   |")
    print ("---|---|---")
    print ("   |   |")
    print (" "+board[4]+" | "+board[5]+" | "+board[6]+"  ")
    print ("   |   |")
    print ("---|---|---")
    print ("   |   |")
    print (" "+board[7]+" | "+board[8]+" | "+board[9]+"  ")
    print ("   |   |   ")

while True:
    os.system("clear")
    printTitle()
    printBoard()
    choice = input("Please choose an empty space for X. ").upper()
    choice = int(choice)
    if board[choice] == " ":
        board[choice] = "X"
    else:
        print "Sorry, that space is not empty!"
        time.sleep(1)
结果应该是:

   |   |
   |   |
   |   |
-------------
   |   |
   |   |
   |   |
-------------
   |   |
   |   |
   |   |
来自@Prune的错误消息:

文件so.py,第20行 ^ SyntaxError:无效语法 试试这个:

str = '''
        ----------------     1 | 2 | 3
        TIC - TAC - TOE      4 | 5 | 6
        ________________     7 | 8 | 9

        TO PLAY TIC - TAC - TOE, YOU NEED TO GET THREE IN A ROW.
        YOUR CHOICES ARE BETWEEN 1 TO 9.

        '''
 def printTitle(str):
    print(str)

此函数下的print语句中有一个问题

此错误是由于printTitle方法中print语句中的注释引号导致的,并且没有将字符串放在最后一个print语句的括号中。您需要对打印语句进行更改:

在方法printTitle中,添加删除注释标记并在每行末尾添加开括号以及反斜杠“\”,这用于表示多行字符串。 在while循环中,在上次打印语句中添加括号。 更正后的代码在这里供您参考

import os
import time
import random


board = [" " for x in range(10)]

def printTitle():
    print("\
\
----------------     1 | 2 | 3\
TIC - TAC - TOE      4 | 5 | 6\
________________     7 | 8 | 9\
\
TO PLAY TIC - TAC - TOE, YOU NEED TO GET THREE IN A ROW.\
YOUR CHOICES ARE BETWEEN 1 TO 9.")


def printBoard():
    print ( "   |   |   ")
    print (" "+board[1]+" | "+board[2]+" | "+board[3]+"  ")
    print ("   |   |")
    print ("---|---|---")
    print ("   |   |")
    print (" "+board[4]+" | "+board[5]+" | "+board[6]+"  ")
    print ("   |   |")
    print ("---|---|---")
    print ("   |   |")
    print (" "+board[7]+" | "+board[8]+" | "+board[9]+"  ")
    print ("   |   |   ")

while True:
    os.system("clear")
    printTitle()
    printBoard()
    choice = input("Please choose an empty space for X. ").upper()
    choice = int(choice)
    if board[choice] == " ":
        board[choice] = "X"
    else:
        print("Sorry, that space is not empty!")
        time.sleep(1)

它不在某个地方-您会收到一条带有行号和发生位置文本的特定错误消息。这里的问题是您忘记了printTitle中的print命令中的括号。请发布回溯@Prune,这个打印完全可以在我使用python 2.7的机器上运行。他的代码可能没有对齐。@Torxed:不是重复-这是一个简单的语法错误,不是一个整体输出设计。我试图使用python 3而不是python 2.7。print是python 3中的一个函数,所以请更改print。。。。。。印刷。。。。。。。
import os
import time
import random


board = [" " for x in range(10)]

def printTitle():
    print("\
\
----------------     1 | 2 | 3\
TIC - TAC - TOE      4 | 5 | 6\
________________     7 | 8 | 9\
\
TO PLAY TIC - TAC - TOE, YOU NEED TO GET THREE IN A ROW.\
YOUR CHOICES ARE BETWEEN 1 TO 9.")


def printBoard():
    print ( "   |   |   ")
    print (" "+board[1]+" | "+board[2]+" | "+board[3]+"  ")
    print ("   |   |")
    print ("---|---|---")
    print ("   |   |")
    print (" "+board[4]+" | "+board[5]+" | "+board[6]+"  ")
    print ("   |   |")
    print ("---|---|---")
    print ("   |   |")
    print (" "+board[7]+" | "+board[8]+" | "+board[9]+"  ")
    print ("   |   |   ")

while True:
    os.system("clear")
    printTitle()
    printBoard()
    choice = input("Please choose an empty space for X. ").upper()
    choice = int(choice)
    if board[choice] == " ":
        board[choice] = "X"
    else:
        print("Sorry, that space is not empty!")
        time.sleep(1)