zoukankan      html  css  js  c++  java
  • 用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()

    下面是运行结果:

  • 相关阅读:
    微信小程序代码大全
    【活动发布】捷微H5-微信新年砍价活动,开源发布了
    小程序官网CMS开源项目出炉,Weixin-App-CMS 1.0 版本正式发布
    jeecg开源项目的IDEA的部署
    1024程序员节宅男节日快乐 -- JAVA快速开发平台,JEECG 3.8宅男优化版本发布
    微信小程序商城开源项目,Weixin-App-Shop 1.0 版本正式发布!!!
    JEECG-Swagger UI的使用说明
    JEECG 上传插件升级-代码生成器
    Why 0.1 + 0.2 === 0.30000000000000004 ?
    linux (一)
  • 原文地址:https://www.cnblogs.com/jeavenwong/p/8270485.html
Copyright © 2011-2022 走看看