python_学生信息管理实例
"""提示:代码中的内容均被注释,请参考,切勿照搬"""
"""注意:代码切勿照搬,错误请留言指出"""
1 class Student: 2 def __init__(self,No,name,sex,age): 3 self.No = No 4 self.name = name 5 self.sex = sex 6 self.age = age 7 8 def show(self): 9 print("%s %s %s %d"%(self.No,self.name,self.sex,self.age)) 10 11 class StudentList: 12 def __init__(self): 13 self.students=[] #存信息的空list 14 15 def show(self): 16 print("%s %s %s %d" % (No,name,sex,age)) 17 for s in self.students: 18 s.show() 19 20 def __insert(self,s): 21 i = 0 22 while (i<len(self.students) and s.No> self.students[i].No): ##!! 23 i=i+1 24 if (i<len(self.students) and s.No== self.students[i].NO): ##!! 25 print(s.No+"已经存在") 26 return False 27 self.students.insert(i,s) 28 print("添加成功") 29 return True 30 31 def __update(self,s): 32 flag = False 33 for i in range(len(self.studenlist)): 34 if(s.No == self.students[i].No): 35 self.students[i].name = s.name 36 self.students[i].sex = s.sex 37 self.students[i].age = s.age 38 print("修改成功") 39 flag = True 40 break 41 if (not flag): 42 print("没有这个学生") 43 return flag 44 45 def __delete(self,No): 46 flag = False 47 for i in range(len(self.students)): 48 if (self.students[i].No == No): 49 del self.students[i] 50 print("删除成功") 51 flag = True 52 break 53 if(not flag): 54 print("没有这个学生") 55 return flag 56 57 #用户预输入数据 58 def insert(self): 59 No = input("NO:") 60 name = input("Name:") 61 while True: 62 sex = input("Sex:") 63 if(sex=='男' or sex=='女'): 64 break 65 else: 66 print("----Sex数据有误----") 67 age = int(input("Age:")) 68 if(No!='' or name!=''): 69 self.__insert(Student(No,name,sex,age)) 70 else: 71 print("----学号or性别不能为空----") 72 73 def update(self): 74 No = input("NO:") 75 name = input("Name:") 76 while True: 77 sex = input("Sex:") 78 if (sex == '男' or sex == '女'): 79 break 80 else: 81 print("----Sex数据有误----") 82 age = int(input("Age:")) 83 if (No != '' or name != ''): 84 self.__update(Student(NO, name, sex, age)) 85 else: 86 print("----学号or性别不能为空 ----") 87 88 def delete(self): 89 No = input("No:") 90 if(No != ''): 91 self.__delete(No) 92 #用户输入 93 def scanf(self): 94 while True: 95 s=input(">>>") 96 if (s =='insert'): 97 self.insert() 98 elif (s =='update'): 99 self.update() 100 elif (s =='delete'): 101 self.delete() 102 elif (s =='show'): 103 self.show() 104 elif (s =='exit'): 105 break 106 else: 107 print("----指令输入错误----") 108 109 s=StudentList() 110 s.scanf()