python学生管理システムの作成(オブジェクト向け)

17528 ワード

このプログラムにはグラフィックスインタフェースがなく、データは保存されません.pycharmで実行するか、exeファイルにパッケージしてcmdで実行できます.
import os
class Student(object):
    def print_menu(self):
        """    """  #       
        print("=" * 50)
        print("        V1.0")
        print("1、      ")
        print("2、      ")
        print("3、      ")
        print("4、      ")
        print("5、        ")
        print("6、      ")
        print("0、    ")
        print("=" * 50)
    def add_infor(self):
        new_name = input("     :")
        new_sex = input("     :")
        new_phone = input("      :")
        new_class = input("     :")
        #     
        new_infor = {}
        #             
        new_infor["name"] = new_name
        new_infor["sex"] = new_sex
        new_infor["phone"] = new_phone
        new_infor["class"] = new_class
        #              。
        #        :append()   、insert()  、extend()    
        global card_infors
        card_infors.append(new_infor)

    def find_infor(self):
        """        """
        find_name = input("           :")
        #            ,   name             
        global card_infors
        for temp in card_infors:
            if temp["name"] == find_name:  #    
                #        
                print("  :{}\t  :{}\t   :{}\t  :{}".format(temp["name"], temp["sex"], temp["phone"], temp["class"]))
                break
        else:
            print("{}   ".format(find_name))

    def modify_infor(self):
        """        """
        old_name = input("         :")
        new_name = input("       :")
        new_sex = input("       :")
        new_phone = input("        :")
        new_class = input("       :")
        #          
        global card_infors
        for temp in card_infors:
            if temp["name"] == old_name:
                temp["name"] = new_name
                temp["sex"] = new_sex
                temp["phone"] = new_phone
                temp["class"] = new_class
                print("    !")
                break
        else:
            print("{}   ,    !".format(old_name))

    def delete_infor(self):
        """          """
        del_name = input("        :")
        #              
        global card_infors
        for temp in card_infors:
            if temp["name"] == del_name:  #          
                #        :del[  ]、pop、remove(  )
                card_infors.remove(temp)
                print("    !")
                break
        else:
            print("{}   ,    !".format(del_name))

    def show_infor(self):
        """      """
        global card_infors
        if len(card_infors) > 0:
            print("      :")
            for temp in card_infors:
                #             
                print("  :{}\t  :{}\t   :{}\t  :{}".format(temp["name"], temp["sex"], temp["phone"], temp["class"]))
        else:
            print("        !")

    def save_infor(self):
        """           backup.data """
        try:
            with open("backup.data", "w") as save_file:
                save_file.write(str(card_infors))
        except Exception as err:
            print("     :", err)
            print("    ")
        else:
            print("    !")

    def load_infor(self):
        """    backup.txt     card_infors"""
        if os.path.isfile("backup.data"):
            try:
                with open("backup.data") as f:
                    content = f.read()
                    global card_infors
                    card_infors = eval(content)
            except:
                pass

card_infors = []
stu=Student()
while True:
    stu.print_menu()
    try:
        stu.load_infor()
        num=int(input('       :'))
        if num==1:
            stu.add_infor()
        elif num==2:
            stu.find_infor()
        elif num==3:
            stu.modify_infor()
        elif num==4:
            stu.delete_infor()
        elif num==5:
            stu.show_infor()
        elif num==6:
            stu.save_infor()
        elif num==0:
            quitconfirm = input(" ,       (yes  no)")
            if quitconfirm == 'yes':
                print("    ,    。")
                break
        else:
            print("    ,     。")
    except ValueError:
        print('     。')