Score Entry and Query: Build a Practical Student Score Tool

Introduction

In this chapter, you will build a practical Python mini system for score entry and query. The program will let users input student names and Chinese scores, search scores by name, and find highest and lowest scores. This exercise combines dictionaries, loops, conditions, and simple menu design into one real workflow.

Prerequisites

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

Project Goal

Implement a console program that can:

  • input student name and Chinese score
  • query one student's Chinese score by name
  • show highest Chinese score among recorded students
  • show lowest Chinese score among recorded students

This is close to real student-management features in simple school systems.

1) Data Structure Design

Use a dictionary to store score data:

  • key: student name
  • value: Chinese score
python
# Create empty score dictionary
scores = {}
 
# Example insertion
scores["Emma"] = 95
scores["Liam"] = 88
 
# Print current data
print(scores)

Why dictionary:

  • fast lookup by student name
  • clear mapping between name and score

2) Core Functions

It is cleaner to separate logic into functions.

python
# Add or update one student's score
def add_score(score_map):
    # Read name
    name = input("Enter student name: ").strip()
 
    # Read score with validation
    try:
        score = float(input("Enter Chinese score: "))
    except ValueError:
        print("Invalid score. Please enter a number.")
        return
 
    # Save to dictionary
    score_map[name] = score
    print(f"Saved: {name} -> {score}")
 
 
# Query one student's score
def query_score(score_map):
    # Read target name
    name = input("Enter student name to query: ").strip()
 
    # Safe lookup
    score = score_map.get(name)
    if score is None:
        print("Student not found.")
    else:
        print(f"{name}'s Chinese score: {score}")

3) Highest and Lowest Score Queries

Use max() and min() based on dictionary values.

python
# Show highest score entry
def show_highest(score_map):
    if not score_map:
        print("No records yet.")
        return
 
    # Find name with maximum score
    top_name = max(score_map, key=score_map.get)
    print(f"Highest score: {top_name} -> {score_map[top_name]}")
 
 
# Show lowest score entry
def show_lowest(score_map):
    if not score_map:
        print("No records yet.")
        return
 
    # Find name with minimum score
    low_name = min(score_map, key=score_map.get)
    print(f"Lowest score: {low_name} -> {score_map[low_name]}")

Tip

Useful Pattern

max(dict_obj, key=dict_obj.get) is a very practical pattern for ranking-style queries.

4) Menu Loop

Use a while loop to build a simple interactive menu.

python
# Run score management menu
def run_menu():
    # Store all scores
    scores = {}
 
    while True:
        print("\n=== Student Chinese Score System ===")
        print("1. Add/Update score")
        print("2. Query score by name")
        print("3. Show highest score")
        print("4. Show lowest score")
        print("5. Show all records")
        print("0. Exit")
 
        choice = input("Choose an option: ").strip()
 
        if choice == "1":
            add_score(scores)
        elif choice == "2":
            query_score(scores)
        elif choice == "3":
            show_highest(scores)
        elif choice == "4":
            show_lowest(scores)
        elif choice == "5":
            show_all(scores)
        elif choice == "0":
            print("Bye!")
            break
        else:
            print("Invalid option. Please choose again.")

5) Full Runnable Version

Here is the complete script:

python
# Add or update one student's score
def add_score(score_map):
    # Read name
    name = input("Enter student name: ").strip()
 
    # Read score with validation
    try:
        score = float(input("Enter Chinese score: "))
    except ValueError:
        print("Invalid score. Please enter a number.")
        return
 
    # Validate score range
    if score < 0 or score > 100:
        print("Score must be between 0 and 100.")
        return
 
    # Save to dictionary
    score_map[name] = score
    print(f"Saved: {name} -> {score}")
 
 
# Query one student's score
def query_score(score_map):
    # Read target name
    name = input("Enter student name to query: ").strip()
 
    # Safe lookup
    score = score_map.get(name)
    if score is None:
        print("Student not found.")
    else:
        print(f"{name}'s Chinese score: {score}")
 
 
# Show highest score entry
def show_highest(score_map):
    if not score_map:
        print("No records yet.")
        return
 
    # Find name with maximum score
    top_name = max(score_map, key=score_map.get)
    print(f"Highest score: {top_name} -> {score_map[top_name]}")
 
 
# Show lowest score entry
def show_lowest(score_map):
    if not score_map:
        print("No records yet.")
        return
 
    # Find name with minimum score
    low_name = min(score_map, key=score_map.get)
    print(f"Lowest score: {low_name} -> {score_map[low_name]}")
 
 
# Show all records
def show_all(score_map):
    if not score_map:
        print("No records yet.")
        return
 
    print("=== All Student Scores ===")
    for name, score in score_map.items():
        print(f"{name}: {score}")
 
 
# Run score management menu
def run_menu():
    # Store all scores
    scores = {}
 
    while True:
        print("\n=== Student Chinese Score System ===")
        print("1. Add/Update score")
        print("2. Query score by name")
        print("3. Show highest score")
        print("4. Show lowest score")
        print("5. Show all records")
        print("0. Exit")
 
        choice = input("Choose an option: ").strip()
 
        if choice == "1":
            add_score(scores)
        elif choice == "2":
            query_score(scores)
        elif choice == "3":
            show_highest(scores)
        elif choice == "4":
            show_lowest(scores)
        elif choice == "5":
            show_all(scores)
        elif choice == "0":
            print("Bye!")
            break
        else:
            print("Invalid option. Please choose again.")
 
 
# Program entry
run_menu()

Warning

Student names are used as dictionary keys here, so duplicate names will overwrite previous scores.
If you need to support duplicate names, add unique IDs later.

Common Beginner Mistakes

Mistake 1: Not Checking Empty Data Before max()/min()

Calling max() or min() on an empty dictionary raises an error.

Mistake 2: Forgetting Numeric Validation

If user types invalid score input, your program can crash without try/except.

Mistake 3: Ignoring Score Range

Always validate score range (0-100) to keep data meaningful.

Surprise Practice Challenge

Upgrade this system with ranking output:

  1. Add menu option 6. Show ranking (high to low)
  2. Sort by score descending
  3. Print ranking index + name + score
  4. Highlight top 3 with Gold, Silver, Bronze

If you finish this, you are building a mini grade-management system, not just a demo.

FAQ

Why use dictionary for this task?

Dictionary provides direct lookup by student name, which is perfect for query-by-name features.

Can one student have multiple subjects?

Yes. You can use nested dictionaries, such as scores[name] = {"chinese": 90, "math": 85}.

How can I keep data after program exits?

You can save records to a file (for example JSON or CSV) and load it on startup.

What if two students share the same name?

Use unique student IDs as keys, and store names as normal fields.