我正在研究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))),)
您忘记让函数
plan_nba_helper
返回任何内容。我认为您希望它return newGames
,也可能希望在elif
语句中return plan_nba_helper(newGames)
相关问题 更多 >
编程相关推荐