How to store multiple entries for each student using dictionaries

Here's a description of the problem:

There is an "n" student record, each record has the student's name, percentage marks obtained in Maths, Physics and Chemistry. The user enters an integer "n" followed by names and labels for the students "n". I need to store an entry in a dictionary data type. The user then enters the student's name, and you should print the average percentage of that student's grades to two decimal places. what i have tried so far:

num_students = int(raw_input("Please enter number of students:"))
print "you entered %s students" %num_students
student_info = {}
student_data = ['studentname', 'mathmarks', 'physicsmarks', 'chemistrymarks']
for i in range(0,num_students):
    for entry in student_data:
        student_info[entry] = raw_input(entry )
print student_info
print"please enter student name"
name = raw_input("student name")
if student_info['studentname'] == name:
    print "Average student marks:", (int(student_info['mathmarks']) + int(student_info['physicsmarks']) + int(student_info['chemistrymarks']))/3
else:
    print"please enter valid name"

      

This code works num_students = 1, but if num_students> 1 the code fails.

I cannot save each student's entry in the dictionary.

I am new to python, would be glad if someone can help me with this.

+3


source to share


3 answers


Actually you need to create a nested dictionary with a name as values ​​and another dict as keys, in short a nested dict might look like this:

{
    'anmol': {'chemistrymarks': 3, 'physicsmarks': 2, 'mathmarks': 1},
    'uppal': {'chemistrymarks': 6, 'physicsmarks': 5, 'mathmarks': 4}
}

      



So, you need to add the following lines to create a nested dictionary.

num_students = int(raw_input("Please enter number of students:"))
print "you entered %s students" %num_students
student_info = {}
student_data = ['Math marks : ', 'Physics marks : ', 'Chemistry marks : ']
for i in range(0,num_students):
    student_name = raw_input("Name :")
    student_info[student_name] = {}
    for entry in student_data:
        student_info[student_name][entry] = int(raw_input(entry)) #storing the marks entered as integers to perform arithmetic operations later on.
#print student_info
print"Please enter student name ?"
name = raw_input("Student name : ")
if name in student_info.keys():
    print "Average student marks : ", str(sum(student_info[name].values())/3.0)
else:
    print"please enter valid name"

      

+4


source


  #youcan use print stmts. acording to your problem
 n = raw_input()
    grades = []
    for entry in range(int(n)):
        grades.append([i for i in raw_input().split()])
    query = raw_input()

    # Find list where first item matches name in query and
    # assign grades to queryResult
    queryResult = [x[1:] for x in grades if x[0] == query]

    total = 0
    scores = 0

for x in queryResult:
    for y in x:
        total += float(y)
        scores += 1
print "%.2f" % (float(total/scores))

      



0


source


#Another way
num_of_student = int(raw_input())
dir_student = {}

for i in range(0,num_of_student):
    student_info = raw_input()
    name = student_info.split()
    dir_student[name[0]] = [float(name[1]),float(name[2]),float(name[3])]

find_name = raw_input()
if dir_student.has_key(find_name):
    print "{0:.2f}".format(sum(dir_student[find_name])/3.0)

      

0


source







All Articles