简体   繁体   中英

Python returning none instead of string

This is my code for reference

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

The print statement accurately prints the expected output which is:

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 

However, my return value for the same variable is None. I don't quite understand why.

Any help would be appreciated

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

You're never printing the result of output_file() . You're printing the result of tally() , but it doesn't return anything. If you want that to print the result of output_file() , change:

output_file()

to:

return output_file()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM