以前用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()

下面是运行结果:

最新文章

  1. ASP.NET MVC5+EF6+EasyUI 后台管理系统(34)-文章发布系统①-简要分析
  2. bootstrap 学习
  3. LinearLayout布局问题
  4. Opensource开源精神
  5. C#调用Python 脚本语言
  6. GDB基本调试
  7. Apache 多站点(虚拟主机)
  8. ajaxFileUpload 注意!
  9. simplify the life ECMAScript 5(ES5)中bind方法简介
  10. 帝国cms中上一篇与下一篇个性化灵动标签调出
  11. Explicit keyword
  12. 移动端日历控件 mobiscroll 的简单使用、参数设置
  13. “==”与"equals(object)"的区别
  14. Scala 对象
  15. Java图片加文字水印
  16. 【图片版】学习CSS网格布局
  17. 方法总结:如何实现html页面自动刷新
  18. 剑指Offer_编程题_25
  19. linux关于软件安装的博文
  20. Mac 安装Python3 facewap环境

热门文章

  1. (八)mybatis之多对多
  2. ASP.NET Core 中的脚本标记帮助程序
  3. logback日志详细解析
  4. harbor小结
  5. Orion (system-on-a-chip)
  6. Linux行编辑器——ed
  7. 搭建KVM环境——06 创建虚拟机
  8. Linux学习笔记(十一)shell基础:管道符、通配符和其他特殊符号
  9. 各位大佬Python的第一部分道基础题已经整理好了,希望大家面试的时候能用的上。
  10. jade-if-else-unless-case