以前用C++和Java写过学生管理系统,也想用Python试试,果然“人生苦短,我用Python”。用Python写的更加简洁,实现雏形也就不到100行代码。

下面上代码

 #!/usr/bin/python3
# coding=utf-8 # 实现switch-case语句
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args: # changed for v1.5, see below
self.fall = True
return True
else:
return False class student:
def __init__(self, name, age, id, grade):
self.next = None
self.name = name
self.age = age
self.id = id
self.grade = grade def show(self):
print('name:', self.name, ' ', 'age:', self.age, ' ', 'id:', self.id, ' ', 'grade:', self.grade) class stulist:
def __init__(self):
self.head = student('', 0, 0, 0) def display(self):
p = self.head.next
if p==None:
print('no student data in database!')
while p:
p.show()
p = p.next def insert(self):
print('please enter:')
name = input('name:')
age = input('age:')
id = input('id:')
grade = input('grade:')
newstu = student(name, age, id, grade)
p = self.head
while p.next:
p = p.next
p.next = newstu def query(self):
name = input('please enter the name you want to query:')
p = self.head.next
if p==None:
print('no such student in database')
while p:
if p.name == name:
p.show()
p = p.next def delete(self):
name = input("please enter the student'name you want to delete:")
p = self.head.next
pre = self.head
while p:
if p.name == name:
pre.next = p.next
p.next = None
print('the student info has been deleted!')
break
else:
pre = p
p = p.next def main():
stulist1 = stulist()
user_input = input('please enter the OPcode:')
while user_input:
print('a--insert/b--display/c--query/h or ''--help/d--delete')
for case in switch(user_input):
if case('a'):
stulist1.insert()
user_input = input('please enter the OPcode:')
break
if case('b'):
stulist1.display()
user_input = input('please enter the OPcode:')
break
if case('c'):
stulist1.query()
user_input = input('please enter the OPcode:')
break
if case('d'):
stulist1.delete()
user_input = input('please enter your OPcode:')
break
if case(): # default
print('please enter the OPcode...')
user_input = input('please enter the OPcode:')
break if __name__ == "__main__":
main()

下面是运行结果:

用python实现简易学生管理系统-LMLPHP

05-07 09:24