如何在函数中传递字典列表?

How to pass a list of dictionaries in a function?

我正在使用 Django 开发教师评分系统。我想要一些功能,其中有一些来自前端的 subject id 和学生 marks 之类的条目。我在后端的应用程序采用这两个参数并创建一个包含 subject idmarks 的字典列表并将其传递给另一个函数,该函数将总结所有标记并给我一个总计和下一个平均值和百分比等。但是现在,我只能使用总计,所以当我在函数中传递这个字典列表时,它会给我一个错误。

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)

错误是:

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> 

您的 class 有一些问题。首先,由于您使用了 b.mark_calculation(),我假设您倾向于在 class 中定义此函数,但现在不是!因此,在 class 上调用它是错误的。你应该这样称呼它:

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 

其次,您调用了 marks_calculation 而没有引用 class。如果您尝试调用 class 的函数,在大多数情况下,您应该使用 self 以便调用对象本身的函数。这意味着您的代码应该类似于:

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

第三,你调用了 class 而没有调用 () 这似乎是错误的。你应该使用类似的东西:

# Rest of code
b= Marks_entry()

第四,我无法理解您使用 b.marks_calculation(marks, subject_id) 的意图,因为您将此函数定义为仅获取一个参数(仅 marks 而不是 subject_id )。如果你想传递更多的变量给你的函数,你应该在调用函数之前在函数中定义它们。

根据您新编辑的问题,您当前的代码存在多个问题 -

  • 有些地方的缩进不正确。
  • try/except 块应该在 while 循环下面
  • 既然要先把所有条目都放在marks_entry中,就应该先初始化它,然后再给它append数据。目前,它会在您获得每个新条目后重置回 []。因此,当您尝试对其求和时,它始终最多包含 1 个元素。
  • 一旦退出 while 循环
  • ,最后应该计算总数

这是在您的代码之上构建的示例代码 -

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}')

示例输出:

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