Ascii Art in Python not printing in one line

那年仲夏 提交于 2019-12-13 06:27:41

问题


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)

I'm trying to make a chess board, but the cell left keeps printing in separate lines. Also the spaces in between the | is needed, but it need to be next to the |. FIXED

NEW PROBLEM Now my out put has a space every two lines and I have updated the code above.

The output is suppose to look like this:

|--|--|--|--|--|--|--|--|
|  |  |  |  |  | X|  |  |
|--|--|--|--|--|--|--|--|
|  |  | X|  |  |  |  |  |
|--|--|--|--|--|--|--|--|
|  |  |  |  | X|  |  |  | 
|--|--|--|--|--|--|--|--|
|  |  |  |  |  |  |  | X|
|--|--|--|--|--|--|--|--|
| X|  |  |  |  |  |  |  |
|--|--|--|--|--|--|--|--|
|  |  |  | X|  |  |  |  |
|--|--|--|--|--|--|--|--|
|  | X|  |  |  |  |  |  |
|--|--|--|--|--|--|--|--|
|  |  |  |  |  |  | X|  |
|--|--|--|--|--|--|--|--|

I know this chess board isn't very square but this is only a rough draft at the moment.


回答1:


print() in Python 3 prints newlines unless you tell it not to. Pass in end='' to tell it not to print that newline:

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("|")


来源:https://stackoverflow.com/questions/21654443/ascii-art-in-python-not-printing-in-one-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!