用python实现简易学生管理系统
以前用C++和Java写过学生管理系统,也想用Python试试,果然“人生苦短,我用Python”。用Python写的更加简洁,实现雏形也就不到100行代码。
下面上代码
1 #!/usr/bin/python3 2 # coding=utf-8 3 4 5 # 实现switch-case语句 6 class switch(object): 7 def __init__(self, value): 8 self.value = value 9 self.fall = False 10 11 def __iter__(self): 12 """Return the match method once, then stop""" 13 yield self.match 14 raise StopIteration 15 16 def match(self, *args): 17 """Indicate whether or not to enter a case suite""" 18 if self.fall or not args: 19 return True 20 elif self.value in args: # changed for v1.5, see below 21 self.fall = True 22 return True 23 else: 24 return False 25 26 27 class student: 28 def __init__(self, name, age, id, grade): 29 self.next = None 30 self.name = name 31 self.age = age 32 self.id = id 33 self.grade = grade 34 35 def show(self): 36 print('name:', self.name, ' ', 'age:', self.age, ' ', 'id:', self.id, ' ', 'grade:', self.grade) 37 38 39 class stulist: 40 def __init__(self): 41 self.head = student('', 0, 0, 0) 42 43 def display(self): 44 p = self.head.next 45 if p==None: 46 print('no student data in database!') 47 while p: 48 p.show() 49 p = p.next 50 51 52 def insert(self): 53 print('please enter:') 54 name = input('name:') 55 age = input('age:') 56 id = input('id:') 57 grade = input('grade:') 58 newstu = student(name, age, id, grade) 59 p = self.head 60 while p.next: 61 p = p.next 62 p.next = newstu 63 64 def query(self): 65 name = input('please enter the name you want to query:') 66 p = self.head.next 67 if p==None: 68 print('no such student in database') 69 while p: 70 if p.name == name: 71 p.show() 72 p = p.next 73 74 def delete(self): 75 name = input("please enter the student'name you want to delete:") 76 p = self.head.next 77 pre = self.head 78 while p: 79 if p.name == name: 80 pre.next = p.next 81 p.next = None 82 print('the student info has been deleted!') 83 break 84 else: 85 pre = p 86 p = p.next 87 88 89 def main(): 90 stulist1 = stulist() 91 user_input = input('please enter the OPcode:') 92 while user_input: 93 print('a--insert/b--display/c--query/h or ''--help/d--delete') 94 for case in switch(user_input): 95 if case('a'): 96 stulist1.insert() 97 user_input = input('please enter the OPcode:') 98 break 99 if case('b'): 100 stulist1.display() 101 user_input = input('please enter the OPcode:') 102 break 103 if case('c'): 104 stulist1.query() 105 user_input = input('please enter the OPcode:') 106 break 107 if case('d'): 108 stulist1.delete() 109 user_input = input('please enter your OPcode:') 110 break 111 if case(): # default 112 print('please enter the OPcode...') 113 user_input = input('please enter the OPcode:') 114 break 115 116 117 if __name__ == "__main__": 118 main()
下面是运行结果:
少一些功利主义的追求,多一些不为什么的坚持!