There is a list with nested dictionaries.

GRADEBOOK = [ {'name':'Maksim Fedartsou', 'subjects': {'math':9,'language':5,'history':8,'foreignl':9}}, {'name':'Pavel Bobkou', 'subjects': {'math':3,'language':8,'history':4,'foreignl':9}}, {'name':'Ann Sokolova', 'subjects': {'math':10,'language':8,'history':4,'foreignl':8}} ] 

First, I get the values ​​for each student, after which I get in which order these grades are written to the out list.

I want to do the following with the grade for each student: the one that the student received at the exam * 0.7 and add the marks of all the other students for the same subject.

The problem is that I did and it works only for the first student, but he doesn’t go on working, or rather, he thinks that everything is over.

Here is the code with comments:

 GRADEBOOK = [ {'name':'Maksim Fedartsou', 'subjects': {'math':9,'language':5,'history':8,'foreignl':9}}, {'name':'Pavel Bobkou', 'subjects': {'math':3,'language':8,'history':4,'foreignl':9}}, {'name':'Ann Sokolova', 'subjects': {'math':10,'language':8,'history':4,'foreignl':8}} ] out= [] for l in range( len( GRADEBOOK ) ): b = [ i for i in GRADEBOOK[l]['subjects'].values() ] out.append( b ) kl = GRADEBOOK[0]['subjects'].keys() mark = 0 marks = [] for n in range(4): # B out я вывел оценки студентов,здесь они должны посчитаться для рейтинговой, то есть для каждого for i in out: #Студента 4 оценки, сколько и предметов, но почему он считает только для одного первого студента, if mark==0:#u как сделать чтоб считал для всех дальше mark= i[n]*0.7 else: mark+= i[n] marks.append( mark )#этот список должен содержать вложенные списки с рейтинговыми оценками для студентов mark = 0 print( marks ) 
  • 3
    Please make the code a text, not a screenshot. very colorful and beautiful, but do not do so. - ReinRaus
  • And, okay, I still have nothing to do :) I made a recognition of a cool screenshot with lighting and comments. - ReinRaus

1 answer 1

Remarks on the code:

  1. It is wrong to sort through dictionaries. You iterate over several dictionaries with the same keys, but it is not guaranteed that the order of the keys will be constant. Something like this needs to be written:

     sciences = GRADEBOOK[0]['subjects'].keys() for l in range( len( GRADEBOOK ) ): b = [ GRADEBOOK[l]['subjects'][i] for i in sciences ] 
  2. You wrote the correct algorithm in the human language, but when translated into a programming language, you implemented something completely different. If you read the human-readable algorithm and understand that it correctly solves the problem, then do not break the algorithm, but simply shift it to the desired programming language.

  3. You wrote Я хочу сделать с оценками по предметам для каждого студента следующее . From this phrase, written in ordinary Russian, it follows that the first cycle of enumeration will be the enumeration of students, the enumeration of subjects will be embedded in it.
  4. You wrote the ту которую студент получил на экзамене*0.7 и прибавить оценки всех остальных студентов по этому же предмету this also means that you need to do another sorting over the same subject . It should be borne in mind that if the assessment belongs to this student, then it must be multiplied by 0.7, and if it is different, then simply add to the amount in question.

Explanation of action in the comments:

 GRADEBOOK = [ {'name':'Maksim Fedartsou', 'subjects': {'math':9,'language':5,'history':8,'foreignl':9}}, {'name':'Pavel Bobkou', 'subjects': {'math':3,'language':8,'history':4,'foreignl':9}}, {'name':'Ann Sokolova', 'subjects': {'math':10,'language':8,'history':4,'foreignl':8}} ] sciences = GRADEBOOK[0]['subjects'].keys() # ['math', 'foreignl', 'history', 'language'] result = {} # полностью создаем результирующую структуру и заполняем for student in GRADEBOOK: # ее начальными значениями - нулями result[ student['name'] ] = {} for i in sciences: result[ student['name'] ][i] = 0 # result = {'Pavel Bobkou': {'foreignl': 0, 'math': 0, 'history': 0, 'language': 0}, 'Ann Sokolova': {'foreignl': 0, 'math': 0, 'history': 0, 'language': 0}, 'Maksim Fedartsou': {'foreignl': 0, 'math': 0, 'history': 0, 'language': 0}} for student in GRADEBOOK: # перебираем студентов for i in sciences: # перебираем предметы обучения for j in GRADEBOOK: # снова перебираем студентов, но на самом деле брать будем только его оценку по перебираемому предмету обучения if student == j: result[ student['name'] ][i]+= j['subjects'][i]*0.7 # если оценка принадлежит перебираемому студенту, то умножаем ее на 0.7 и прибавляем к итогу else: result[ student['name'] ][i]+= j['subjects'][i] # не принадлежит ему - просто прибавляем print(result) 

Result:

 { 'Maksim Fedartsou': {'foreignl': 23.3, 'history': 13.6, 'math': 19.3, 'language': 19.5}, 'Ann Sokolova': {'foreignl': 23.6, 'history': 14.8, 'math': 19.0, 'language': 18.6}, 'Pavel Bobkou': {'foreignl': 23.3, 'history': 14.8, 'math': 21.1, 'language': 18.6} }