所以我设法创建了一个程序,该程序可用于创建具有可配置行和列的棋盘:
def print_column_labels():
print " a b c d e f g h"
def print_separator_row(num_columns):
print " " +"+-" * num_columns+"+"
def print_squares_white(row_number, num_columns):
if row_number<=9:
print " "+str(row_number) +" "+"| |#" * (num_columns / 2)+"| "+str(row_number)
else:
print " "+ str(row_number ) +""+"|#| " * (num_columns / 2)+"| "+str(row_number)
def print_squares_black(row_number, num_columns):
if row_number<=9:
print " "+ str(row_number ) +" "+"|#| " * (num_columns / 2)+"| "+str(row_number)
else:
print " "+ str(row_number ) +""+"|#| " * (num_columns / 2)+"| "+str(row_number)
def print_chessboard(row_num,num_columns):
print_column_labels()
print_separator_row(num_columns)
if row_num%2==0:
while row_num>=1:
print_squares_white(row_num,num_columns)
row_num-=1
print_separator_row(num_columns)
print_squares_black(row_num,num_columns)
row_num-=1
print_separator_row(num_columns)
if row_num==0:
print_column_labels()
else:
while row_num>=2:
print_squares_white(row_num,num_columns)
row_num-=1
print_separator_row(num_columns)
print_squares_black(row_num,num_columns)
row_num-=1
print_separator_row(num_columns)
if row_num==1:
print_squares_white(row_num,num_columns)
print_separator_row(num_columns)
print_column_labels()
我需要对程序做的下一件事是将其从仅打印板更改为将其作为字符串返回。我该如何更改代码?