-2

所以我设法创建了一个程序,该程序可用于创建具有可配置行和列的棋盘:

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()

我需要对程序做的下一件事是将其从仅打印板更改为将其作为字符串返回。我该如何更改代码?

4

3 回答 3

2

要从函数返回,请使用return

def myFunc(x):
    return x+2

var = myFunc(2)
print var # prints 4

如果您print改为使用以下方法会发生这种情况:

def myFunc(x):
    print x+2

var = myFunc(2) # prints 4
print var # prints None

这是因为如果return没有在 python 函数中调用,python 会return None在函数末尾自动假定您想要。不确定这些信息是否有帮助,但如果有帮助,它就在那里。

于 2013-10-04T00:56:54.997 回答
0
def print_column_labels():
    return "   a b c d e f g h" + "\n"

def print_separator_row(num_columns):
    return "   " +"+-" * num_columns+"+" + "\n"

def print_squares_white(row_number, num_columns):
    if row_number<=9:
        return " "+str(row_number) +" "+"| |#" * (num_columns / 2)+"| "+str(row_number) + "\n"
    else:
        return " "+ str(row_number ) +""+"|#| " * (num_columns / 2)+"| "+str(row_number) + "\n"

def print_squares_black(row_number, num_columns):
    if row_number<=9:
        return " "+ str(row_number ) +" "+"|#| " * (num_columns / 2)+"| "+str(row_number) + "\n"
    else:
        return " "+ str(row_number ) +""+"|#| " * (num_columns / 2)+"| "+str(row_number) + "\n"

def print_chessboard(row_num,num_columns):
    result = ""
    result = result + print_column_labels()
    result = result + print_separator_row(num_columns)
    if row_num%2==0:
        while row_num>=1:
            result = result + print_squares_white(row_num,num_columns)
            row_num-=1
            result = result + print_separator_row(num_columns)
            result = result + print_squares_black(row_num,num_columns)
            row_num-=1
            result = result + print_separator_row(num_columns)
            if row_num==0:
                result = result + print_column_labels()
    else:
        while row_num>=2:
            result = result + print_squares_white(row_num,num_columns)
            row_num-=1
            result = result + print_separator_row(num_columns)
            result = result + print_squares_black(row_num,num_columns)
            row_num-=1
            result = result + print_separator_row(num_columns)
            if row_num==1:
                result = result + print_squares_white(row_num,num_columns)
                result = result + print_separator_row(num_columns)
                result = result + print_column_labels()

    return result

chessboard = print_chessboard(8,8)

print chessboard
于 2013-10-04T01:06:58.207 回答
0

您想在函数中构建字符串,然后在最后返回它们。

def get_column_labels():
    return "   a b c d e f g h"

def get_chessboard(row_num, num_columns):
    # start with an empty list
    rows = []
    # append the result of other functions to the list
    rows.append(get_column_labels()) 

    # ...

    # build the rows into a string and return it
    return '\n'.join(rows)
于 2013-10-04T01:08:03.350 回答