BugLab018 / 020up to 100
loopsenumerateoff-by-oneeasy

Wrong Rank

The function rank_players(scores) takes a list of (name, score) tuples — already sorted highest to lowest — and returns a list of ranking strings like "1. Alice: 95", "2. Bob: 87".

The function runs without errors, but all the rank numbers are off by one. Every player shows rank 0, 1, 2... instead of 1, 2, 3.

One small parameter change is all it takes.

# your fix
+ must call enumerate()
hint 2after hint 1
hint 3after hint 2
solution

Read the code. Before running anything: what does the last line below give?

def rank_players(scores):
"""
Takes a list of (name, score) tuples already sorted from highest to
lowest and returns a list of ranking strings.
Ranks start at 1, not 0.
Example: rank_players([("Alice", 95), ("Bob", 87)])
-> ["1. Alice: 95", "2. Bob: 87"]
"""
result = []
for i, (name, score) in enumerate(scores):
result.append(f"{i}. {name}: {score}")
return result
>>> rank_players([('Alice', 95), ('Bob', 87)])