Python中的Ascii艺术不是在一个lin中打印

2024-04-25 18:49:38 发布

您现在位置:Python中文网/ 问答频道 /正文

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


def row_top():
    print("|--|--|--|--|--|--|--|--|")

def cell_left():
   print("| ", end = "")

def solution(s):
   for i in range(8):
       row(s[i])

def cell_data(isQ):
   if isQ:
      print("X", end = "")
      return ()
   else:
      print(" ", end = "")


def row_data(c):
   for i in range(9):
      cell_left()
      cell_data(i == c)

def row(c):
   row_top()
   row_data(c)
   print("\n")


solution(s)

我在试着做一个棋盘,但是左边的细胞一直在分开打印。也需要|之间的空格,但它必须紧挨着|。修正

新问题 现在我的输出每两行有一个空格,我已经更新了上面的代码。在

假设输出如下所示:

^{pr2}$

我知道这个棋盘不是很方正,但这只是一个粗略的草稿。在


Tags: infordata棋盘topdefcellrange
1条回答
网友
1楼 · 发布于 2024-04-25 18:49:38

python3中的print()将打印新行,除非您告诉它不要这样做。传入end=''以告诉它不要打印该换行符:

def row_top():
    print("| | | | | | | | |")

def cell_left():
     print("| ", end='')

def cell_data(isQ):
     if isQ:
        print("X", end='')
    else:
        print(" ", end='')

def row(c):
    row_top()
    row_data(c)
    print("|")

相关问题 更多 >

    热门问题