Python返回none而不是string

2024-06-26 13:40:42 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码供参考

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 

但是,同一变量的返回值为“无”。我不太明白为什么。你知道吗

任何帮助都将不胜感激


Tags: inifresultwinresultsentryteamsbadgers
2条回答

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

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

output_file()

收件人:

return output_file()

相关问题 更多 >