简体   繁体   中英

How do I return a tuple of tuples in python? i.e. ((((1, 8), (4, 5)), ((2, 7), (3, 6))),) It keeps returning None

I'm working on a recursive algorithm in Python 3 that produces a list containing a tuple of tuples. When I try to return the list, I get none. Can anyone spot what I'm missing? Here is what I have:

def plan_nba_playoff(num_teams):
    games =list(range(1, num_teams+1))
    return plan_nba_helper(games)

def plan_nba_helper(teams):
    newGames = []
    c = len(teams) - 1
    for i in range(len(teams)//2):
        game = teams[i], teams[c],
        newGames.append(game)
        c -= 1
        if i == c and len(newGames) == 1:
            print(newGames)
            return newGames
        elif i == c:
            plan_nba_helper(newGames) #((((1, 8), (4, 5)), ((2, 7), (3, 6))),)

print(plan_nba_playoff(8)) #((((1, 8), (4, 5)), ((2, 7), (3, 6))),)

You forgot to make your function plan_nba_helper return anything. I think you want it to return newGames and also maybe the in the elif statement you may want to return plan_nba_helper(newGames) .

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