'Finding Percentages marks

We have a record of students. Each record contains the student's name, and their percent marks in Maths, Physics and Chemistry. The marks can be floating values. The user enters some integer followed by the names and marks for students. We are required to save the record in a dictionary data type. The user then enters a student's name. Output the average percentage marks obtained by that student, correct to two decimal places.

I have tried the following but I am getting an error for some n values

if __name__ == '__main__':
    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()
    query_score=student_marks[query_name]
    a=(sum(query_score)/n)
    print("{0:.2f}".format(a))


Solution 1:[1]

Try that:

if __name__ == '__main__':
    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()
    query_score=student_marks[query_name]
    a=(sum(query_score)/len(query_score))
    print("{0:.2f}".format(a))

Solution 2:[2]

if __name__ == '__main__':
    n = int(input())
    marks = {}
    avg = 0
    scores_sum = 0
    for i in range(n):
        name, *line = input().split()
        scores = list(map(float, line))
        marks[name] = scores
   
    query_name = input()
    for i in marks[query_name]:
        scores_sum += i
    
    avg = scores_sum/len(marks[query_name])
    print("{:.2f}".format(avg))
    
        
           
 I was able to execute the above code and it is working fine now.

Solution 3:[3]

In Python-3

if __name__ == '__main__':
    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()

print("%.2f" %(sum(student_marks[query_name])/len(student_marks[query_name])))

Input:

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

Output:

56.00

Solution 4:[4]

if __name__ == '__main__':
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()


print("{0:.2f}".format(round(sum(student_marks[query_name]) / len(student_marks[query_name]), 2)))

Solution 5:[5]

if __name__ == '__main__':
n = int(input())
student_names = {}
stud_name1 =[]
stud_name =[]
final_marks=[]

for studentInfo in range(n):
    stud_name = list(input().split())
    
    stud_name1.append(stud_name)
final_name = input()
      
for m in stud_name1:
    student_names.update({m[0]:{'maths':float(m[1]),'physics':float(m[2]),'chemistry':float(m[3])}})
for n in student_names:
    if final_name == n:
        for subject_name in student_names.get(n):
            final_marks.append(student_names.get(n).get(subject_name))

average = (final_marks[0]+final_marks[1]+final_marks[2])/3
formatted_float = "{:.2f}".format(average)
print(formatted_float)

Solution 6:[6]

Try this:

if __name__ == '__main__':
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()
average = sum(student_marks[name])/len(student_marks[name])
average = format(average,'.2f')
print(average)

Solution 7:[7]

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()
query_score = student_marks[query_name]
a = (sum(query_score)/len(scores))
print("{0:.2f}".format(a))

Solution 8:[8]

I solved this challenge code:

if __name__ == '__main__':
    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()
    sum=0
    avg=0
    for key, value in student_marks.items():
      if (key==query_name):
        for i in value:
          sum=sum+i
        avg=sum/len(value)
    print(format(avg,'.2f'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AnsFourtyTwo
Solution 2 kaushal kukreja
Solution 3 Biman Pal
Solution 4 Apurv Mishra
Solution 5 Pravin Kumar
Solution 6 Ian
Solution 7 Avinash kumar
Solution 8 mehrdad rafiee