+ 1
How do u calculate average mark for a maximum of 5 entries using a dictionary
student={} for i in range(5): name=input() mark=int(input) student.update({'name':name,'mark':mark}) print(student)
6 Réponses
+ 1
well you loop over the dictionary keys, add the marks value and divide it by 5.
i. e.
sum=0
for i in student:
sum+=student[i]
print(sum/5)
And it is "mark=int(input())"
+ 1
simbarashe
Is this 5 students with 1 mark each? Which is what the code you have posted does. If so you can just use the sum() function on a list of the values from the dict, divided by the length of the dictionary.
average_mark = sum(list(student.values())) / len(student)
If this is not the correct structure for your dictionary then please describe further.
+ 1
Would something like this help
students = {
"Bob" : 41,
"Jak" : 62,
"Amy" : 98,
}
total = 0
for i in students:
total += students[i]
average = total / len(students)
print(average)
0
Abhay i have done wat u suggested but still not working
0
simbarashe can you show me your code?
0
student_1 = input()
...
student_5 = input()
sum = student_1 + ... + student_5
avg = sum/5
print(avg)
You can attribute a number for each student. Python also understand Ab>Ac = True. You can put them in order too.