求亚军成绩的Python程序
假设我们有不同数量参与者的分数列表。我们必须找到亚军的分数。
因此,如果输入类似于分数=[5,8,2,6,8,5,8,7],那么输出将是7,因为获胜者分数是8,第二大分数是7。
示例
让我们看下面的实现来更好地理解
def solve(scores): winner = -99999 runner_up = -99999 for i in scores: if (i > winner): winner, runner_up = i, winner elif (i < winner and i > runner_up): runner_up = i return runner_up scores = [5,8,2,6,8,5,8,7] print(solve(scores))
输入
[5,8,2,6,8,5,8,7]输出结果
7