欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python_学生信息管理实例

程序员文章站 2024-01-07 20:43:58
"""提示:代码中的内容均被注释,请参考,切勿照搬""" """注意:代码切勿照搬,错误请留言指出""" ......

"""提示:代码中的内容均被注释,请参考,切勿照搬"""

"""注意:代码切勿照搬,错误请留言指出"""


 

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

 

上一篇:

下一篇: