python學生信息管理系統(文件操作方式)

對python文件操作最好的練手方式莫過於寫個學生信息管理系統了。該學生信息管理系統實現了對學生信息的存儲,修改,刪除,查看,搜索,清空功能。話不多說,開始開車XD...

首先,定義學生類:

class StudentInfo:
    def __init__(self, name, stuid, gender, age, major, tel):
        self.name = name
        self.stuid = stuid
        self.gender = gender
        self.age = age
        self.major = major
        self.tel = tel

    def info(self):
        # 字符串的拼接
        if self.gender == '1':
            self.gender = 'Male'
        else:
            self.gender = 'Female'
        msg = self.name + ' ' + self.stuid + ' ' + self.gender + ' ' + self.age + ' ' + self.major + ' ' + self.tel + '\n'
        return msg

其次,各種文件操作功能函數定義:

# add student information. version1: simply store the string
def add():
    if_continue = '1'
    global flag
    flag = 1
    # the file has been created, appending,and will be closed automatically
    while if_continue == '1':
        with open('student.txt', 'a') as f:
            name = input("Name: ")
            stuid = input("Student ID: ")
            gender = input("1.Male  2.Female : ")
            age = input("Age: ")
            major = input("Major: ")
            tel = input("Telephone: ")
            student = StudentInfo(name, stuid, gender, age, major, tel)
            f.write(student.info())
            print("Adding successfully!")
            print(student.info(), end='')
        if_continue = input("Press 0 to stop, 1 to continue.\n")


def delete():
    if_continue = '1'
    global flag
    flag = 1
    while if_continue == '1':
        with open('student.txt', 'r+') as f:
            discard = input("delete the info you want: ")
            lines = f.readlines()
            f.seek(0)
            for line in lines:
                if discard not in line:
                    f.write(line)
                else:
                    print("Delete successfully!")
                    print(line, end='')
            f.truncate()
        if_continue = input("Press 0 to stop, 1 to continue.\n")


def revise():
    if_continue = '1'
    global flag
    flag = 1
    while if_continue == '1':
        with open('student.txt', 'r+') as f:
            revise_line = input("show the info line: ")
            lines = f.readlines()
            f.seek(0)
            for line in lines:
                if revise_line in line:
                    print(line, end='')
                    old_info = input("old info: ")
                    new_info = input("new info: ")
                    line = line.replace(old_info, new_info)
                    f.write(line)
                    print("Revise successfully!")
                    print(line, end='')
                else:
                    f.write(line)
            f.truncate()
        if_continue = input("Press 0 to stop, 1 to continue.\n")


# if using the os.remove you delete the file
def clear():
    if_continue = '1'
    global flag
    flag = 1
    while if_continue == '1':
        with open("student.txt", 'r+') as f:
            f.truncate()
        print("Clear successfully!")
        if_continue = input("Press 0 to menu.\n")


def research():
    if_continue = '1'
    key = 1
    global flag
    flag = 1
    while if_continue == '1':
        with open("student.txt", 'r') as f:
            search = input("info you want to search: ")
            lines = f.readlines()
            for line in lines:
                if search in line:
                    key = 2
                    print(line, end='')
            if key != 2:
                print("No such student!")
        if_continue = input("Press 0 to stop, 1 to continue.\n")


def view():
    if_continue = '1'
    global flag
    flag = 1
    while if_continue == '1':
        with open("student.txt", 'r') as f:
            lines = f.readlines()
            for line in lines:
                print(line, end='')
        if_continue = input("Press 0 to the menu.\n")

再者,菜單界面功能定義:

def menu():
    global flag
    while flag == 1:
        flag = 0
        print("********MENU*********")
        print("*** 1.Add         ***")
        print("*** 2.Delete      ***")
        print("*** 3.revise      ***")
        print("*** 4.clear       ***")
        print("*** 5.research    ***")
        print("*** 6.view        ***")
        print("*** 7.exit        ***")
        print("*********************")
        num = input("Function: ")  # more convenient using dictionary?
        if num == '1':
            add()
        elif num == '2':
            delete()
        elif num == '3':
            revise()
        elif num == '4':
            clear()
        elif num == '5':
            research()
        elif num == '6':
            view()
        elif num == '7':
            logout()
        else:
            flag = 1
            print("Wrong input!")


def logout():
    print("Are you sure to log out?")
    print("1.Yes", end=' ')
    print("2.No")
    if input("yes or not: ") == '1':
        exit()
    else:
        pass


def login():
    user = input("Input the user: ")
    password = input("Input the password: ")
    if user == User and password == Password:
        menu()
    else:
        print("The user name or the password is wrong!")


def start():
    print("*************************************************")
    print("*** Welcome to the student management system! ***")
    print("*** 1.log in                                  ***")
    print("*** 2.exit                                    ***")
    print("*************************************************")

最後,主函數定義:

def main():
    while True:
        start()
        num = input("Choose the number: ")
        if num == '1':
            login()
        elif num == '2':
            logout()
        else:
            print("Wrong! Enter again!")

運行結果:

 

代碼源碼直通車:https://download.csdn.net/download/weixin_42194402/12082218

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章