如何在字典Python中为每个学生的列表添加成绩

问题描述 投票:-1回答:1
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次附加成绩。

python arrays dictionary append
1个回答
0
投票

您可以将成绩添加到列表中,只需将其添加到词典中的对应学生中

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()

0
投票

您总是覆盖它,并且永远不会在适当的列表中填写成绩。

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()
© www.soinside.com 2019 - 2024. All rights reserved.