小姐姐学Python-class3-4

综合项目-学生管理系统

非常基础的功能,实现系统的登录,学生的增删改查,用到了类和实例的概念,结合了前面读取文本中信息,使用字典和用户登录的方法。

要求:
1. 熟悉类和实例的基本用法
2. 复习之前学过的知识

代码:

  1. account.txt

  2. student.py

class Student(object):
    def __init__(self,sid,name,gender,mclass,region):
        self.sid = sid
        self.name = name
        self.gender = gender
        self.mclass = mclass
        self.region = region
        self.grade = {}
    def setGrade(self,grade):
        self.grade = grade
    def toString(self):
        return self.sid + " " + self.name + " " + self.gender + " " + self.mclass +" " + self.region

  1. main.py
#main.py
from student import Student

students = []
def getAccount():
    f = open('./account.txt','r')

    whole = f.read()
    list_line = whole.split('\n')
    #print(list_line)
    dic = {}
    for line in list_line:
        account  = line.split(' ')[0]
        password = 'NULL'
        if len(line.split(' ')) > 1:
            password  = line.split(' ')[1]
        dic[account] = password
    return dic


    #print('shortname: ',short_name,'full_name : ',full_name)

def login():
    dic = getAccount()
    account = ''
    password = ''

    try_time = 3
    while try_time > 0:
        account = input('请输入账号')
        password = input('请输入密码')
        for k,v in dic.items():
            if k == account and v == password:
                print('登录成功,欢迎: ',account)
                return True
        try_time -= 1
        print('账号或者密码错误,你还有 ',try_time,'次机会')

    print('登录失败')
    return False




def addStudent():
    global students
    sid = input('请输入学生学号')
    name = input('请输入学生姓名')
    gender = input('请输入学生性别')
    mclass = input('请输入学生班级')
    region = input('请输入学生籍贯')
    student = Student(sid,name,gender,mclass,region)
    students.append(student)
    #print(students[0].toString())
    print('添加成功!,添加的学生信息如下')
    print(student.toString())

def deleteStudent():
    global students
    sid = input('请输入要删除的学生学号')
    isFind = False
    cnt = 0
    for student in students:
        if student.sid == sid:
            students.pop(cnt)
            print('删除成功')
            isFind = True
            return
        cnt += 1
    print('对不起,没有找到您输入的学号')

def updateStudent():
    global students
    sid = input('请输入要修改的学生学号')
    isFind = False
    cnt = 0
    for student in students:
        if student.sid == sid:
            student.sid = input('请输入新的学号')
            student.name = input('请输入新的学生姓名')
            student.gender = input('请输入新的学生性别')
            student.mclass = input('请输入新的学生班级')
            student.region = input('请输入新的学生籍贯')
            students[cnt] = student
            print('修改成功')
            isFind = True
            return
        cnt += 1
    print('对不起,没有找到您输入的学号')

def printStudents():
    print()
    print("-----学生列表如下-----")
    print('sid name gender mclass region')
    for student in students:
        print(student.toString())
    print()
def searchStudent():
    global students
    print('请输入下面的编号')
    print('1.通过学号')
    print('2.通过姓名')
    print('3.通过班级')

    order = int(input())
    if order == 1:
        sid = input('请输入要查找的学生学号')
        isFind = False
        cnt = 0
        for student in students:
            if student.sid == sid:
                print('找到了',student.toString())
                isFind = True
            cnt += 1
        if isFind == False:
            print('对不起,没有找到您输入的学号')
    elif order == 2:
        name = input('请输入要查找的学生姓名')
        isFind = False
        cnt = 0
        for student in students:
            if student.name == name:
                print('找到了',student.toString())
                isFind = True
            cnt += 1
        if isFind == False:
            print('对不起,没有找到您输入的学号')
    elif order == 3:
        mclass = input('请输入要查找的学生班级')
        isFind = False
        cnt = 0
        for student in students:
            if student.mclass == mclass:
                print('找到了',student.toString())
                isFind = True
            cnt += 1
        if isFind == False:
            print('对不起,没有找到您输入的学号')
    else:
        print('输入有误')


def main():
    print('欢迎使用学生管理系统') 
    if login():
        while True:
            print()
            print('-----学生管理系统-----')
            print('1.添加学生')
            print('2.删除学生')
            print('3.修改学生')
            print('4.查找学生')
            print('5.查看所有学生')
            print('6.退出')
            print('--------------------')
            print()
            order = int(input('请输入编号,进行对应的操作'))
            if order == 1:
                addStudent()
            elif order == 2:
                deleteStudent()
            elif order == 3:
                updateStudent()
            elif order == 4:
                searchStudent()
            elif order == 5:
                printStudents()
            elif order == 6:
                exit()
            else:
                print('输入有误,请重试')

if __name__ == '__main__':
    main()

# 将来要做的
# 
# 将这个程序,改成每次启动时,能从本地读取系统的数据,且退出时能保存数据(每一步操作都保存数据到本地)


点赞
  1. 图图说道:

    :mrgreen: 非常好的帖子,得到了非常好的练习,顶一下

    1. chaiwenjun000说道:

      :cool: