"""提示:代碼中的內容均被註釋,請參考,切勿照搬""" """註意:代碼切勿照搬,錯誤請留言指出""" ...
"""提示:代碼中的內容均被註釋,請參考,切勿照搬"""
"""註意:代碼切勿照搬,錯誤請留言指出"""
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()