获得返回而不是打印的功能

时间:2013-10-04 00:53:22

标签: python python-2.7 return

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

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

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

3 个答案:

答案 0 :(得分: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

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

答案 1 :(得分: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

答案 2 :(得分: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)