【问题标题】:How to Append Grades on List per student in Dictionary Python如何在字典 Python 中为每个学生的列表附加成绩
【发布时间】:2020-04-09 09:32:56
【问题描述】:
Students = {}

def IslemYap():
    Input = int(input("Process Number: "))
    if Input == 1:
        StudentName = input("Student Name: ")
        for i in range(1,8):
            Students.update({StudentName:[input()]})
            print(Students)

IslemYap()

我正在尝试这个但不工作。每个学生 7 次追加成绩。

【问题讨论】:

  • for循环应该是加成绩的吧?
  • 欢迎来到 Stack Overflow。 “不工作”是什么意思?请阅读How to Ask
  • 变量和函数名应该遵循lower_case_with_underscores风格。

标签: python arrays dictionary append


【解决方案1】:

您可以将成绩添加到列表中,然后将其添加到字典中的对应学生

Students = {}

def IslemYap():
    Input = int(input("Process Number: "))
    grades = []
    if Input == 1:
        StudentName = input("Student Name: ")
        for i in range(1,8):
            grades.append(input())
        Students.update({StudentName: grades})
        grades = []
        print(Students)

IslemYap()

【讨论】:

    【解决方案2】:

    你总是覆盖它,从不将成绩填写在正确的列表中。

    Students = {}
    
    def IslemYap():
        Input = int(input("Process Number: "))
        if Input == 1:
            StudentName = input("Student Name: ")
            for i in range(1,8):
                Students[StudentName] = Students.get(StudentName,[]) +  [input()]
                print(Students)
    
    IslemYap()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 2016-01-02
      • 2011-01-10
      • 2021-09-20
      • 1970-01-01
      相关资源
      最近更新 更多