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)

チェス盤を作ろうとしていますが、残っているセルが別々の行に印刷され続けます。| の間のスペースも が必要ですが、| の隣にある必要があります。修繕

新しい問題 出力に 2 行ごとにスペースがあり、上記のコードを更新しました。

出力は次のようになります。

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

このチェス盤があまり正方形ではないことは知っていますが、これは現時点ではラフ ドラフトにすぎません。

4

1 に答える 1

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("|")
于 2014-02-09T02:23:48.010 に答える