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
.pyfiles 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
# 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.
3) Highest and Lowest Score Queries
Use max() and min() based on dictionary values.
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.
5) Full Runnable Version
Here is the complete script:
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:
- Add menu option
6. Show ranking (high to low) - Sort by score descending
- Print ranking index + name + score
- 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.