2014-02-09 101 views
0
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) 

我试图制作一个国际象棋棋盘,但单元格保持单独打印行。也在|之间的空格是必要的,但它需要在|旁边。 固定Python中的Ascii艺术不在同一行中打印

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

输出是假设是这样的:

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

我知道这个棋盘不是很广场但这只是目前初稿。

+0

如果您有新问题,提出新的问题!不要编辑问题,以便给出的答案不再有意义。我正在回滚该编辑。 – SethMMorton

+0

我的情况你不明白,我的意思是问一个[全新的问题](http://stackoverflow.com/questions/ask)。不仅修改这个现有的问题来询问一个全新的问题不会遵循这个网站的协议,*没有人会回答它,因为这个问题已经有了答案。通过实际遵循我的指示(假设你真的希望问题得到解答),你会为自己做一件事。 – SethMMorton

回答

0

print()在Python 3中打印新行,除非你不告诉它。通过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("|") 
+0

假设OP使用打印功能(这在这里看起来很合理)。如果它实际上是一个带有多余括号的打印语句,则通过添加尾随逗号来实现换行符抑制。 – mgilson

+0

它的工作谢谢!,但我有一个新的问题,现在在每一行之间我有一个空间。 – pakiboii