The simplest analysis compares two groups. Work out which group scored higher, and by how much.
What to build
A program that compares the average scores of two groups.
Requirements
- Print
A: average X (N students)and then the same for B, with the average to two decimals; writestudentinstead ofstudentswhen N is 1. - Then print
A did better by D pointsorB did better by D points, with D to two decimals, orno differenceif the averages are equal to two decimals.
Input
Line 1: n. Then n lines GROUP SCORE, where GROUP is A or B.
Output
Three lines.
Constraints
1 ≤ n ≤ 100; each group has at least one score; scores are between 0 and 100.
Example
Input:
5
A 70
B 65
A 80
B 75
A 90
Output:
A: average 80.00 (3 students)
B: average 70.00 (2 students)
A did better by 10.00 points
Why it works
Comparing averages rather than totals makes groups of different sizes comparable, and stating each group's size tells the reader how much the averages can be trusted.
Examples
Example 1 · Input
5 A 70 B 65 A 80 B 75 A 90
Expected output
A: average 80.00 (3 students) B: average 70.00 (2 students) A did better by 10.00 points
Example 2 · Input
4 A 50 B 60 A 70 B 60
Expected output
A: average 60.00 (2 students) B: average 60.00 (2 students) no difference
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Keep a list of scores for each group in a dictionary: {"A": [], "B": []}.
Hint 2
Round the difference to two decimals before deciding whether there is one.
Solution
Show one correct solution (try it yourself first)
n = int(input())
scores = {"A": [], "B": []}
for _ in range(n):
group, score = input().split()
scores[group].append(float(score))
average = {}
for group in ("A", "B"):
values = scores[group]
average[group] = sum(values) / len(values)
word = "student" if len(values) == 1 else "students"
print(f"{group}: average {average[group]:.2f} ({len(values)} {word})")
difference = round(average["A"] - average["B"], 2)
if difference > 0:
print(f"A did better by {difference:.2f} points")
elif difference < 0:
print(f"B did better by {-difference:.2f} points")
else:
print("no difference")