Produce a ranked report for a whole class.
What to build
A complete report with ranking and a summary.
Requirements
- Read each student's name and three marks.
- Print the students from highest average to lowest. Equal averages are in alphabetical order.
- Each line is
RANK. NAME AVERAGE GRADE, with the average to one decimal. - The grades are
A+for 80 or more,Afor 70 or more,Bfor 60 or more, and otherwiseC. - Finally, print
A+: W A: X B: Y C: Z, the number of students with each grade.
Input
The first line has n. Each of the next n lines is a name and three marks.
Output
One line per student, then the summary.
Constraints
n is from 1 to 100, and marks are from 0 to 100. Names are different.
Example
Input:
2
Rahim 72 68 80
Nadia 91 88 95
Output:
1. Nadia 91.3 A+
2. Rahim 73.3 A
A+: 1 A: 1 B: 0 C: 0
Why it works
A tuple key sorts by the negative average first and the name second, which handles both rules at once. Starting all four grade counts at 0 means that unused grades still appear in the summary.
Examples
Example 1 · Input
2 Rahim 72 68 80 Nadia 91 88 95
Expected output
1. Nadia 91.3 A+ 2. Rahim 73.3 A A+: 1 A: 1 B: 0 C: 0
Example 2 · Input
1 Mitu 10 20 30
Expected output
1. Mitu 20.0 C A+: 0 A: 0 B: 0 C: 1
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Store (name, average) for each student.
Hint 2
Sort with key=lambda s: (-s[1], s[0]).
Hint 3
Keep a dictionary of how many students got each grade, starting every grade at 0.
Solution
Show one correct solution (try it yourself first)
def grade(avg):
if avg >= 80:
return "A+"
if avg >= 70:
return "A"
if avg >= 60:
return "B"
return "C"
n = int(input())
students = []
for _ in range(n):
parts = input().split()
marks = [int(x) for x in parts[1:]]
students.append((parts[0], sum(marks) / len(marks)))
counts = {"A+": 0, "A": 0, "B": 0, "C": 0}
students.sort(key=lambda s: (-s[1], s[0]))
for rank, (name, avg) in enumerate(students, start=1):
letter = grade(avg)
counts[letter] += 1
print(f"{rank}. {name} {avg:.1f} {letter}")
print(" ".join(f"{k}: {v}" for k, v in counts.items()))