Python 3D Academyবাংলা

Project: a class grade book

🌍 In real life. At the end of term, Ms Rahman has a sheet of marks: each student's Bangla, English and Maths. She needs each student's average and grade, the top student, the class average for each subject, and who needs extra help because they scored below 33 in a subject. By hand, that's an evening with a calculator.

In this project you'll build her grade book. It uses everything from World 2: loops, lists, unpacking, dictionaries and comprehensions.

The plan in one sentence: read the marks into a dictionary that maps each name to a list of marks, then answer every question with one pass over it.

Its words:

  • data structure: how the data is arranged in memory; here, {"Rimi": [78, 85, 90], ...}
  • record: everything about one student: their name and their marks
  • report: the lines the program prints

Step by step, the plan:

  1. Read: the first line is how many students; each next line is a name and three marks, like Rimi 78 85 90.
  2. Store: name, *marks = line.split() splits a record; the marks become numbers with a comprehension.
  3. Per student: average, grade, and the best average so far.
  4. Per subject: the class average of each column, and everyone with a mark below 33.

After step 3 the averages could sit in their own dictionary, one locker per student:

Steps 1 and 2: read and store. A starred name collects the leftovers (Lesson 9.2), so name, *marks = "Rimi 78 85 90".split() gives name = "Rimi" and marks = ["78", "85", "90"]. They're still strings, so a comprehension turns them into numbers. A dictionary is the right home: you can look a student up by name instantly, and each name appears once.

n = int(input())
book = {}
for _ in range(n):
    name, *marks = input().split()
    book[name] = [int(m) for m in marks]

for name, marks in book.items():
    average = sum(marks) / len(marks)
    print(f"{name}: {average:.1f}")

Step 3: grades and the top student. An if/elif chain turns each average into a grade, strictest first. The top student is the "best so far" pattern from Lesson 11.3. Starting best at -1 is safe because no average can be below 0.

Step 4: subjects. Mark i of every student is one subject's column: [marks[i] for marks in book.values()]. A filtered comprehension finds everyone with a mark below 33.

n = int(input())
book = {}
for _ in range(n):
    name, *marks = input().split()
    book[name] = [int(m) for m in marks]

top = ""
best = -1
for name, marks in book.items():
    average = sum(marks) / len(marks)
    if average >= 80:
        grade = "A+"
    elif average >= 70:
        grade = "A"
    elif average >= 60:
        grade = "B"
    elif average >= 50:
        grade = "C"
    else:
        grade = "F"
    print(f"{name}: {average:.1f} {grade}")
    if average > best:
        best = average
        top = name
print(f"Top: {top}")

subjects = ["Bangla", "English", "Maths"]
for i, subject in enumerate(subjects):
    column = [marks[i] for marks in book.values()]
    print(f"{subject}: {sum(column) / len(column):.1f}")

needs = [name for name, marks in book.items() if min(marks) < 33]
print("Needs help:", ", ".join(needs) if needs else "none")
Rimi averages 84.3, an A+, and is top. Cy's 30 in Bangla is below 33, so Cy needs help.

Why build it this way, and what it costs. The reading loop runs once per student. Each report is one more pass: per student, then per subject. For a class of n students, that's a few passes of n steps, so even a whole school of 2,000 students is instant. Looking a student up by name (book["Rimi"]) is one quick dictionary lookup, however big the school.

Mistakes and edge cases.

  • Two names for three marks. name, marks = line.split() gives ValueError: too many values to unpack (expected 2, got 4). The star collects the rest.
  • Forgetting int(). sum(["78", "85"]) gives TypeError: unsupported operand type(s) for +: 'int' and 'str'.
  • A student with no marks. sum([]) / len([]) divides by zero; the input format promises three marks, but real data might not.
  • Two students with the same name. The second one overwrites the first in the dictionary. Real systems use a roll number as the key.
  • Ties for top. With >, the first student with the best average wins.

Quiz

  1. What does this print?

    name, *marks = "Bo 55 40 62".split()
    print(name, len(marks))
  2. What does this print?

    book = {"Rimi": [78, 85, 90], "Bo": [55, 40, 62]}
    print([marks[1] for marks in book.values()])
  3. Which line stops this program with an error?

    line = "Rimi 78 85 90"
    name, marks = line.split()
    print(name)
  4. Why does the grade book keep the students in a dictionary instead of a list?

    • To find a student by name in one quick step
    • Because a list can't hold numbers
    • To keep the students sorted by mark
  5. Nobody needs help. What does this print?

    needs = []
    print("Needs help:", ", ".join(needs) if needs else "none")

Coding tasks

  1. Step 1: read the class. The first line is the number of students. Each next line is a name and three marks, like Rimi 78 85 90. Store them in a dictionary of name → list of marks, then print each student's average with 1 decimal place, in the order they were read:

    Rimi: 84.3
    Bo: 52.3
    Cy: 56.0
    
  2. Step 2: grades and the top student. After each average, add a grade: A+ for 80 or more, A for 70 or more, B for 60 or more, C for 50 or more, and F below that. After all the students, print the student with the highest average:

    Rimi: 84.3 A+
    Bo: 52.3 C
    Cy: 56.0 C
    Top: Rimi
    
  3. Step 3: the subject report. After the top student, print the class average for each subject (the marks are Bangla, English and Maths, in that order), then everyone with any mark below 33, joined with ", ", or none:

    Bangla: 54.3
    English: 65.7
    Maths: 72.7
    Needs help: Cy