Python中的Ascii艺术不在一行中打印 [英] Ascii Art in Python not printing in one line
问题描述
s = [0,2,6,4,7,1,5,3]
def row_top():
print(| - | - | - | - | - | - | - | - |)
def cell_left():
print (|,end =)
def解决方案:
(8):$ b $ row(s [i])
def cell_data(isQ):
if isQ:
print(X,end =)
return()
else:
print(,end =)
def row_data(c):
(9):
cell_left()
$ row $($)
$($) b
$ b解决方案
我正在努力一个国际象棋棋盘,但左侧的单元格保持分开的行。也在|之间的空格是需要的,但它需要在|旁边。 FIXED
新问题
现在我的输出每两行都有一个空格,上面的代码。
输出结果如下所示:
| - | - | - | - | - | - | - | - |
| | | | | | X | | |
| - | - | - | - | - | - | - | - |
| | | X | | | | | |
| - | - | - | - | - | - | - | - |
| | | | | X | | | |
| - | - | - | - | - | - | - | - |
| | | | | | | | X |
| - | - | - | - | - | - | - | - |
| X | | | | | | | |
| - | - | - | - | - | - | - | - |
| | | | X | | | | |
| - | - | - | - | - | - | - | - |
| | X | | | | | | |
| - | - | - | - | - | - | - | - |
| | | | | | | X | |
| - | - | - | - | - | - | - | - |
我知道这个棋盘不是很方,但这只是一个粗略的草案。 Python 3中的print()打印换行符,除非你告诉它不要。通过 end =''
来告诉它不要打印该换行符:
def row_top():
print(| - | - | - | - | - | - | - | - |)
def cell_left():
print(|,end ='')
def cell_data(isQ):
if isQ:
print(X, (b)
$ b $ row_data(c)
print(|)
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.
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("|")
这篇关于Python中的Ascii艺术不在一行中打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!