在python中如何返回元组的元组?i、 (1,8,(4,5)),(2,7,(3,6)),它一直不返回任何值

2024-04-27 17:35:42 发布

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

我正在研究Python 3中的递归算法,该算法生成一个包含元组的元组列表。当我试图返回列表时,没有得到任何结果。有人能发现我遗漏了什么吗? 以下是我所拥有的:

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

Tags: helper算法列表lenreturndefrangenum
1条回答
网友
1楼 · 发布于 2024-04-27 17:35:42

您忘记让函数plan_nba_helper返回任何内容。我认为您希望它return newGames,也可能希望在elif语句中return plan_nba_helper(newGames)

相关问题 更多 >