Read lines of name,mark. Print the average mark with two decimals, then the name of the top student (the first one if there is a tie).
What to build
A program that turns lines of text into a small report.
Requirements
- Read each
name,markline. - Print the average with two decimals, then the top student.
Input
The first line has n (1 to 1000). Each of the next n lines is name,mark.
Output
Average: A and Top: NAME on two lines.
Constraints
n is from 1 to 1000. Marks are whole numbers.
Example
Input:
3
Rahim,72
Nadia,91
Karim,85
Output:
Average: 82.67
Top: Nadia
Why it works
Splitting each line gives the two fields, and max keeps the first of equal values, which handles a tie.
Examples
Example 1 · Input
3 Rahim,72 Nadia,91 Karim,85
Expected output
Average: 82.67 Top: Nadia
Example 2 · Input
1 Solo,50
Expected output
Average: 50.00 Top: Solo
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
line.split(",") gives the name and the mark as text.
Hint 2
max(rows, key=...) returns the first largest row.
Solution
Show one correct solution (try it yourself first)
n = int(input())
rows = []
for _ in range(n):
name, mark = input().split(",")
rows.append((name, int(mark)))
average = sum(m for _, m in rows) / n
top = max(rows, key=lambda r: r[1])[0]
print(f"Average: {average:.2f}")
print("Top:", top)