Ranking Game: Sort Scores from High to Low

Introduction

In this chapter, you will build a small but practical ranking game with Python. The goal is to sort students' Chinese scores from highest to lowest and display the ranking clearly. This exercise helps you combine lists, loops, and sorting into one complete mini project.

Prerequisites

  • Python 3.10+ installed
  • Basic understanding of lists, tuples, loops, and input/output
  • Ability to run .py files in terminal or IDE

Project Goal

You need to implement this requirement:

  • There are several kindergarten senior-class students
  • Each student has a Chinese score
  • Sort all scores from high to low
  • Show the ranking result

This is very close to real data processing tasks in school systems.

1) Prepare Data

Use a list of tuples to store (name, score).

python
# Student Chinese scores
students = [
    ("Liam", 89),
    ("Emma", 95),
    ("Noah", 78),
    ("Olivia", 92),
    ("Ava", 85),
]
 
# Print original data
print("Original data:")
for item in students:
    print(item)

Why this structure:

  • Tuple keeps one student's data together
  • List keeps all students in one collection

2) Sort by Score Descending

Use sorted() with key and reverse=True.

python
# Sort by score (index 1), high to low
ranked_students = sorted(students, key=lambda item: item[1], reverse=True)
 
# Print sorted result
print("Ranked data:")
for item in ranked_students:
    print(item)

Tip

Key Idea

item[1] means "use score as sorting key."
reverse=True means descending order.

3) Print a Friendly Ranking Table

Now show the ranking in a readable format.

python
# Print ranking title
print("=== Chinese Score Ranking ===")
 
# Print ranking lines
for index, (name, score) in enumerate(ranked_students, start=1):
    print(f"{index}. {name} - {score}")

Example output:

  • 1. Emma - 95
  • 2. Olivia - 92
  • 3. Liam - 89

4) Full Runnable Version

Here is the complete script:

python
# Student Chinese scores
students = [
    ("Liam", 89),
    ("Emma", 95),
    ("Noah", 78),
    ("Olivia", 92),
    ("Ava", 85),
]
 
# Sort by score descending
ranked_students = sorted(students, key=lambda item: item[1], reverse=True)
 
# Print ranking result
print("=== Chinese Score Ranking ===")
for index, (name, score) in enumerate(ranked_students, start=1):
    print(f"{index}. {name} - {score}")

5) Upgrade Version: User Input Mode

You can also let users enter student data manually.

python
# Create empty list for student data
students = []
 
# Ask how many students to input
count = int(input("How many students? "))
 
# Collect each student's data
for i in range(1, count + 1):
    name = input(f"Enter name for student #{i}: ").strip()
    score = float(input(f"Enter Chinese score for {name}: "))
    students.append((name, score))
 
# Sort data by score descending
ranked_students = sorted(students, key=lambda item: item[1], reverse=True)
 
# Print ranking result
print("=== Chinese Score Ranking ===")
for index, (name, score) in enumerate(ranked_students, start=1):
    print(f"{index}. {name} - {score}")

Warning

If two students have the same score, their relative order depends on original input order.
If you need secondary sorting (like name), add another key rule later.

Common Beginner Mistakes

Mistake 1: Sorting in Ascending Order by Accident

For ranking high to low, remember reverse=True.

Mistake 2: Using Wrong Key Index

In (name, score), index 0 is name and index 1 is score.

Mistake 3: Forgetting Type Conversion for Input

input() returns text. Convert score with float() or int() before sorting.

Surprise Practice Challenge

Improve this ranking game with medals:

  • Top 1: Gold
  • Top 2: Silver
  • Top 3: Bronze
  • Others: Participant

Suggested output style:

  • 1. Emma - 95 (Gold)
  • 2. Olivia - 92 (Silver)
  • 3. Liam - 89 (Bronze)

If you finish this, you already have the foundation for leaderboard systems.

FAQ

Should I use sorted() or .sort()?

Use sorted() when you want a new sorted list and keep original data unchanged. Use .sort() when you want to modify the original list directly.

Why use tuple data like (name, score)?

It is simple and readable for fixed two-field records in beginner exercises.

Can scores be decimal values?

Yes. Use float if decimal scores are allowed.

How can I sort by score then by name?

Use a composite key such as key=lambda item: (-item[1], item[0]).