学生信息排序 python 字典删除用不对求助(新手)

问题描述

请设计一个简单的学生成绩管理系统,要求系统实现以下功能:
插入一个学生的信息: Insert id name sex year month day x y z, 其中的参数分别为
学号、姓名、性别、出生日期(年、月、日)、三门课的成绩,成绩为浮点数。

List 输出所有学生信息。

查找学生信息: 
Find id 查找学号为id的学生信息。

Change id newname,newsex,newyear,newmonth,newday,newx,newy,newz 
把学号为id的学生信息修改为
newname,newsex,newyear,newmonth,newday,newx,newy,newz(学号保持不变)

删除学生信息 Delete id 删除学号为id的学生信息

按学号从小到大排序  Sort byid
按出生日期从小到大排序 Sort bybirthday
按总成绩从小到大排序  Sort bysum

退出程序: Quit或者Exit

请注意:姓名的长度不超过20。

输入

输入有多行,每行一条指令,指令格式如下:
Insert id name sex year month day x y z 其中的参数分别为学号、姓名、性别、
出生日期(年、月、日)三门课的成绩,成绩为浮点数。

List 输出所有学生信息。

Find id 查找学号为 id 的学生信息。

Change id newname,newsex,newyear,newmonth,newday,newx,newy,newz 
把学号为id的学生信息修改为
newname,newsex,newyear,newmonth,newday,newx,newy,newz(学号保持不变)

Delete id 删除学号为id的学生信息

Sort byid 按学号从小到大排序并输出

Sort bybirthday 按出生日期从小到大排序

Sort bysum 按总成绩从小到大排序并输出

Quit或者Exit 输出"Good bye!"后结束程序。

 

输入:
Insert 0911001 zhangsan F 1992 3 24 87 78 65
Insert 0911003 Lisi F 1992 5 3 77 72 55
Find 0911002
Find 0911003
Insert 0911001 zhangou M 1992 3 24 98 78 65
Insert 0911002 zhaoliu F 1993 8 8 97 90 55
Change 0911002 zhaoliu M 1990 9 9 90 91 92
Change 0911005 zhaoliu M 1990 9 9 90 91 92
Delete 0911001
Delete 0911006
输出
insert:
0911001 zhangsan F 1992 3 24 87.0 78.0 65.0 76.7 230.0
Insert:
0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0
Find:
Failed
Find:
0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0
Insert:
Failed
Insert:
0911002 zhaoliu F 1993 8 8 97.0 90.0 55.0 80.7 242.0
Change:
0911002 zhaoliu M 1990 9 9 90.0 91.0 92.0 91.0 273.0
Change:
Failed
Delete:
Deleted
Delete:
Failed
class student():

    def __init__(self,id,name,sex,year,month,day,x,y,z):
        self.id = id
        self.name = name
        self.sex=sex
        self.year = int(year)
        self.month = int(month)
        self.day = int(day)
        self.x=float(x)
        self.y=float(y)
        self.z=float(z)
        self.sum1=self.x+self.z+self.y
        self.per=self.sum1/3
    def change(self,name,sex,year,month,day,x,y,z):
        self.name = name
        self.sex = sex
        self.year=int(year)
        self.month=int(month)
        self.day=int(day)
        self.x = float(x)
        self.y = float(y)
        self.z = float(z)
        self.sum1 = self.x + self.z + self.y
        self.per = self.sum1 / 3
    def out(self):
        print('%s %s %s %d %d %d %.1f %.1f %.1f %.1f %.1f'%(self.id,self.name,self.sex,self.year,self.month,self.day,self.x,self.y,self.z,self.per,self.sum1))
a_list= {}
while 1:
    flag = 0
    flag1=0
    a=input().split()
    if a[0]=='Insert':
        print("Insert:")
        if len(a_list)>0:
            for i in range(0,len(a_list)):
                if a_list[i].id==a[1]:
                    flag=1
                    break

        if flag==0:
            a_list[len(a_list)] = student(a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9])
            a_list[len(a_list)-1].out()
        else:print("Failed")
    if a[0] =='Find':
        print("Find:")
        for i in range(0,len(a_list)):
            if a_list[i].id==a[1]:
                flag=1
                a_list[i].out()
                break
        if flag==0:
            print("Failed")

    if a[0] =='Change':
        print("Change:")
        for i in range(0,len(a_list)):
            if a_list[i].id==a[1]:
                flag=1
                a_list[i].change(a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9])
                a_list[i].out()
        if flag==0:
            print("Failed")

    if a[0] =='Delete':
        print("Delete:")
        for i in range(0, len(a_list)):
            print(a_list[i].id)
            if a_list[i].id == a[1]:
                flag = 1
                a_list.pop(i)
                print("Deleted")
                break

        if flag==0:
            print("Failed")
    #if a[0]=='Sort':

    #if a[0]=='Quit' or a[0]== "Exit":
        #break

 Traceback (most recent call last):
  File "E:/pythonProject1/作业.py", line 68, in <module>
    print(a_list[i].id)
KeyError: 0

提示是这个但实际上是pop删除用完后导致的,只要使用了整个字典就坏掉了

我想用这个删除字典中的对应的这个键

 

del a_list[i],,用del这个删除字典的某个键值对吧

如果对你有帮助,可以点击我这个回答右上方的【采纳】按钮,给我个采纳吗,谢谢

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632