'How to pass a list of dictionaries in a function?

I am working on a teacher grading system in Django. I want functionality in which there is some entry like subject id and student's marks from the frontend. My app on the backend takes these two-parameter and creates a list of dictionaries with subject id and marks and pass it on another function and that function will sum up all the marks and give me a total and next average and percentage etc. But right now, I am stuck with total only so, when I pass this list of dictionaries in a function it gives me an error.

def marks_calculation(marks_entry):
       total = sum(item['marks'] for item in marks_entry)
       return total

class Student_marks:
   def entry(subject_id, marks):
      while True:
         value = input("Need to enter marks, Press 'y' for Yes, 'n' for No: ").lower()
         if value == 'n':
            break
      try:
         subject_id = int(input(f'Enter subject id: '))
         marks=int(input(f'Enter marks:  '))
      except ValueError:
            print(f'You can only try integers:')
            continue
      marks_entry=[]
      marks_entry.append({
         "subject_id": subject_id,
         "marks": marks
      })
      total_marks = marks_calculation(marks_entry)
      return total_marks

marks=0
subject_id= 0
b= Students_marks
b.entry(marks, subject_id)

error is:

It is not giving me a total marks
"c:/Users/Lenovo/Documents/TGS/controller.py"
Enter subject id: 1
Enter marks: 58
PS C:\Users\Lenovo\Documents\TGS> 


Solution 1:[1]

There are a few problems with your class. First of all, since you used b.mark_calculation() I assume you tended to define this function within the class, which is not right now! So, calling it on the class would be wrong. you should call it like:

class Marks_entry:
  def marks_entry(subject_id, marks):
      #...
  def marks_calculation(marks_entry):
       total = sum(item['marks'] for item in marks_entry)
       return total 

Second, you called marks_calculation without referring to the class. If you are trying to call a function of class, in most of the cases, you should use self in order to call a function of an object right within itself. Meaning your code should be something like:

class Marks_entry:
  def marks_entry(subject_id, marks):
      # rest of code
      marks_calculation = self.marks_calculation(marks_entry)
      return marks_calculation

  def marks_calculation(self,marks_entry):
       total = sum(item['marks'] for item in marks_entry)
       return total

Third, you call the class without () which seems to be wrong. You should use something like:

# Rest of code
b= Marks_entry()

Fourth, I can't understand your intention of using b.marks_calculation(marks, subject_id) since you have defined this function to get just one argument(just marks and notsubject_id ). If you want to pass more variables to your function, you should define them in the function before calling the function.

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 Amirhossein Kiani