python - Python返回值必须写两次?
问题描述
def simulate_tournament(teams):
if len(teams) == 1:
return teams[0]['team']
else:
teams = simulate_round(teams)
simulate_tournament(teams)
return teams[0]['team']
return teams[0]['team']
如果没有 if else 语句之外的最后一行,我的代码将无法运行。但我不明白为什么我需要这个?len(teams) == 1
在函数返回团队之前,函数不会迭代吗?
解决方案
由于您的 else 块没有return
语句或任何对象来保存数据,因此对于递归调用,它正在调用并没有返回,因此您没有得到任何结果
def simulate_tournament(teams):
if len(teams) == 1:
result= teams[0]['team']
else:
teams = simulate_round(teams)
result = simulate_tournament(teams)
return result
推荐阅读
- c# - a = a + 00001 我想念什么
- c# - 如何在没有循环或jquery的mvc中使用模型类添加动态控件
- javascript - Array.Prototype ForEach 等待回调函数在递归函数中返回
- java - indexOf() 方法中的“%”有什么用?
- c# - Specflow - 使用 .NET Core 的 Microsoft 依赖注入导致 BoDi.ObjectContainerException
- xcode - 签约“xyzApp”需要开发团队
- java - 按列对 CSV 进行排序
- python - 使用双线性插值查询图像 ee 使用 Pytorch 在分数坐标中查找 RGB 值
- javascript - 如何在 Javascript 中对不安全的整数进行单元测试?
- angular - RxJs 不更新 UI 绑定数组变量