问题
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