2013-10-04 57 views
-3

所以我已經成功地創建可用於創建具有可配置的行和列的棋盤程序:獲取函數返回,而不是打印

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

,我需要能夠在接下來的事情與該程序相關的是將其從打印板改爲將其作爲字符串返回。我如何去改變代碼?

+1

,而不是'print',使用'return' –

+0

我試圖取代我到處使用'和'return' print',但我得到的'的輸出中none' – user2843355

+0

你如何使用該功能得到'None'的輸出? – SethMMorton

回答

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

要從函數返回,使用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。不確定這些信息是否有幫助,但是如果有的話。

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)