2
from decimal import Decimal
n = int(input())
student_marks = {}
for _ in range(n):
    name, *line = input().split()
    scores = list(map(float, line))
    student_marks[name] = scores
query_name = input()
avg = Decimal(sum(scores)/3)
avg = round(avg, 2)
print(avg)

Input:

2
Harsh 25 26.5 28
Anurag 26 28 30
Harsh

Expected Output

26.50

My Output

28.00

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.

The first line contains the integer n, the number of students' records. The next n 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.

Input

3
Krishna 67 68 69
Arjun 70 98 63
Malika 52 56 60
Malika

My Output

56.00

Expected Output

56.00

My code works for this input but doesn't work for another input. Could you please try to explain why?

1 Answer 1

2

In your case, you are not using the query correctly. while calculating avg you are always calculating avg score of the last person.

avg = Decimal(sum(scores)/3)

correct way will be something like this:

avg = Decimal(sum(student_marks[query_name])/3)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.