Python不返回任何内容而不是字符串

时间:2019-03-17 16:27:38

标签: python string return

这是我的参考代码

def tally(tournament_results):

    results = tournament_results.split("\n")

    if results != ['']:
        for i in results:
            entry = i.split(";")
            if entry[0] not in teams:
                create_team(entry[0])

            if entry[1] not in teams:
                create_team(entry[1])

            if entry[2].strip() == "win":
                result(entry[0], entry[1], 'decision')
            elif entry[2].strip() == "loss":
                result(entry[1], entry[0], 'decision')
            elif entry[2].strip() == "draw":
                result(entry[0], entry[1], 'draw')

    output_file()


def output_file():
    global teams
    sorted_x = sorted(teams.items(), key=lambda teams: teams[1][4], reverse=True)
    result = template.format("Team", "MP", "W", "D", "L", "P")
    for entry in sorted_x:
        result = result + "\n" + (template.format(entry[0], entry[1][0], entry[1][1], entry[1][2], entry[1][3], entry[1][4]))

    teams = {}

    print(result)
    return result

if __name__ == '__main__':
    print(tally('Allegoric Alaskans;Blithering Badgers;win\n'
                   'Devastating Donkeys;Courageous Californians;draw\n'
                   'Devastating Donkeys;Allegoric Alaskans;win\n'
                   'Courageous Californians;Blithering Badgers;loss\n'
                   'Blithering Badgers;Devastating Donkeys;loss\n'
                   'Allegoric Alaskans;Courageous Californians;win'))

print语句准确打印期望的输出,即:

Team                           | MP |  W |  D |  L |  P 
Devastating Donkeys            |  3 |  2 |  1 |  0 |  7 
Allegoric Alaskans             |  3 |  2 |  0 |  1 |  6 
Blithering Badgers             |  3 |  1 |  0 |  2 |  3 
Courageous Californians        |  3 |  0 |  1 |  2 |  1 

但是,我对于同一变量的返回值为None。我不太明白为什么。

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:3)

return添加tally语句:将output_file()更改为return output_file()

答案 1 :(得分:2)

您永远不会打印output_file()的结果。您正在打印tally()的结果,但不会返回任何结果。如果要打印output_file()的结果,请更改:

output_file()

收件人:

return output_file()