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