Compare each student with the class average.
What to build
A report that uses enumerate and zip together.
Requirements
- Read the names and the marks.
- Print each student as
1. Name: mark above, usingabove,beloworequalto compare with the class average. - Finally print
Top: NAME. If there is a tie, choose the first in the list.
Input
Two lines: names separated by spaces, then the same number of marks.
Output
One line per student, then the top student.
Constraints
There are from 1 to 100 students, and marks are whole numbers from 0 to 100.
Example
Input:
Rahim Nadia Karim
72 91 80
Output:
1. Rahim: 72 below
2. Nadia: 91 above
3. Karim: 80 below
Top: Nadia
Why it works
The average must be known before the loop starts, because the first student is compared with everybody's marks. Unpacking the pair in the for line keeps all three values named.
Examples
Example 1 · Input
Rahim Nadia Karim 72 91 80
Expected output
1. Rahim: 72 below 2. Nadia: 91 above 3. Karim: 80 below Top: Nadia
Example 2 · Input
Mitu 50
Expected output
1. Mitu: 50 equal Top: Mitu
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Work out the average first.
Hint 2
Use enumerate(zip(names, marks), start=1). Each item is a number and a pair.
Hint 3
Write the loop as: for i, (name, mark) in enumerate(...).
Solution
Show one correct solution (try it yourself first)
names = input().split()
marks = [int(x) for x in input().split()]
average = sum(marks) / len(marks)
top = names[0]
best = marks[0]
for i, (name, mark) in enumerate(zip(names, marks), start=1):
if mark > average:
word = "above"
elif mark < average:
word = "below"
else:
word = "equal"
print(f"{i}. {name}: {mark} {word}")
if mark > best:
best = mark
top = name
print("Top:", top)