Table of Contents
ToggleQuestion
The provided code stub will read in a dictionary containing key/value pairs of name:[marks] for a list of students. Print the average of the marks array for the student name provided, showing 2 places after the decimal.
Example
marks key: value pairs are
‘alpha’:[20, 30, 40]
‘beta’:[30, 50, 70]
query_name = ‘beta’
The query_name is ‘beta’. beta’s average score is (30 + 50 + 70)/3 = 50.0.
The query_name is ‘beta’. beta’s average score is .
Input Format
The first line contains the integer , the number of students’ records. The next lines contain the names and marks obtained by a student, each value separated by a space. The final line contains query_name, the name of a student to query.
Constraints
- 2 ≤ n ≤ 10
- 0 ≤ marks[i] ≤ 100
- length of the marks array = 3
Output Format
Print one line: The average of the marks obtained by the particular student correct to 2 decimal places.
Sample Input 0
3
Krishna 67 68 69
Arjun 70 98 63
Malika 52 56 60
Malika
Sample Output 0
56.00
Explanation 0
Sample Input 1
2
Harsh 25 26.5 28
Anurag 26 28 30
Harsh
Sample Output 1
26.50
Solution
n = int(input())
student_marks = {}
for _ in range(n):
name, *marks = input().split()
marks = list(map(float, marks))
student_marks[name] = marks
query_name = input()
if query_name in student_marks:
marks = student_marks[query_name]
average = sum(marks) / len(marks)
print("{:.2f}".format(average))
- Read the number of student records (
n
) from input. - Initialize an empty dictionary
student_marks
to store the marks of each student. - Iterate
n
times and read the name and marks of each student from input. Split the input line by spaces, and convert the marks to a list of floats. - Store the marks as a value in the
student_marks
dictionary, with the name as the key. - Read the
query_name
from input. - If the
query_name
exists in thestudent_marks
dictionary, retrieve the marks list for that name. - Calculate the average by summing the marks and dividing by the length of the marks list.
- Print the average with 2 decimal places using the format specifier
"{:.2f}"
.
If you find anything wrong in this Solution, feel free to reach us in the comment section.