Home > Mobile >  How to pass a list of dictionaries in a function?
How to pass a list of dictionaries in a function?

Time:02-23

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> 

CodePudding user response:

Based on your new edited question, there are multiple issues with your current code -

  • The indentation is incorrect at places.
  • The try/except block should be under the while loop
  • Since you want to first put all the entries in the marks_entry, you should intialize it first and then append data to it. Currently, its being reset back to [] after every new entry that you get. So it will always have at most 1 element in it, when you are trying to sum it up.
  • The total should be calculated in the end once you exit out of the while loop

Here's a sample code built on top of yours -

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

class Student_marks:
    def entry(subject_id, marks):
        marks_entry=[]
        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.append({
             "subject_id": subject_id,
             "marks": marks
            })
        total_marks = marks_calculation(marks_entry)
        return total_marks

marks=0
subject_id= 0
b = Student_marks
total = b.entry(marks, subject_id)
print(f'Your total marks are {total}')

Sample Output:

Need to enter marks, Press 'y' for Yes, 'n' for No: y
Enter subject id: 1
Enter marks:  44
Need to enter marks, Press 'y' for Yes, 'n' for No: y
Enter subject id: 2
Enter marks:  23
Need to enter marks, Press 'y' for Yes, 'n' for No: y
Enter subject id: 3
Enter marks:  12
Need to enter marks, Press 'y' for Yes, 'n' for No: n
Your total marks are 79

CodePudding user response:

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.

  • Related