// Data Structures
Lists Mini Project — Grade Book
What you're building
A grade book for three subjects: Math, English, Biology.
The user adds grades for each subject. The program calculates and displays the average per subject and the overall average.
What you need to know first
- Nested lists
append()sum()andlen()forloopsif / elif / elsewhile Truewithbreakinput()andfloat()
The brief
Build a program that:
- Stores grades in a nested list — one inner list per subject
- Shows a menu:
1 — Math,2 — English,3 — Biology,4 — Show averages,5 — Quit - Adds the grade to the correct subject list
- On option 4 — prints the average for each subject and the overall average
- Handles the case where a subject has no grades yet
Think before you code
- How do you map menu choices 1, 2, 3 to the correct inner list?
- How do you calculate an average from a list of numbers?
- What do you do if a subject list is empty when the user asks for averages?
- How do you calculate the overall average across all subjects?
Your starting point
subjects = ["Math", "English", "Biology"]
grades = [[], [], []]
while True:
print("\n1 — Add Math grade")
print("2 — Add English grade")
print("3 — Add Biology grade")
print("4 — Show averages")
print("5 — Quit")
choice = input("Your choice: ")
# your code here
Expected output
1 — Add Math grade
2 — Add English grade
3 — Add Biology grade
4 — Show averages
5 — Quit
Your choice: 1
Math grade: 8.5
Added.
Your choice: 1
Math grade: 9
Added.
Your choice: 2
English grade: 7
Added.
Your choice: 4
Math: average 8.75
English: average 7.00
Biology: no grades yet.
Overall average: 7.92
Your choice: 5
Done. Out.
Heads up!
- Grades come in as strings from
input()— convert withfloat() - Use the menu choice as an index into
grades— choice"1"maps togrades[0] - Check
len()before dividing — an empty list causes aZeroDivisionError - For the overall average, flatten all grades into one list or sum totals and counts separately
The solution
subjects = ["Math", "English", "Biology"]
grades = [[], [], []]
while True:
print("\n1 — Add Math grade")
print("2 — Add English grade")
print("3 — Add Biology grade")
print("4 — Show averages")
print("5 — Quit")
choice = input("Your choice: ")
if choice in ("1", "2", "3"):
index = int(choice) - 1
grade = float(input(f"{subjects[index]} grade: "))
grades[index].append(grade)
print("Added.")
elif choice == "4":
print()
total_sum = 0
total_count = 0
for i in range(len(subjects)):
if len(grades[i]) == 0:
print(f"{subjects[i]:8} no grades yet.")
else:
avg = sum(grades[i]) / len(grades[i])
print(f"{subjects[i]:8} average {avg:.2f}")
total_sum += sum(grades[i])
total_count += len(grades[i])
if total_count > 0:
print(f"\nOverall average: {total_sum / total_count:.2f}")
elif choice == "5":
print("Done. Out.")
break
else:
print("Invalid choice. Enter 1 to 5.")
// resources
// 0 comments
// No comments yet. Be the first.
// leave a comment