python練習:做一個簡易的課程設計。Student Information Management System


Student Information Management System

    猶記得,大一時候,用C語言做這個課程設計,我特么一口老血都要噴出來,現在用Python做,反而有一種親切感。

    做一個menu菜單,在while循環里調用定義的insert(),delete(),modify(),sort(),display(),exit()等函數。

    

import pickle as p
import os
#Class Item
class Item:
    def __init__(self,name,age,gender,Chinese,Math,English):
        self.name = name
        self.age = age
        self.gender = gender
        self.Math = Math
        self.Chinese = Chinese
        self.English = English

#The main menu of Student Information Management System
def menu():
    print('********************')
    print('1.Insert an item')
    print('2.Delete an item')
    print('3.Modify an item')
    print('4.Display all items')
    print('5.Sort all items')
    print('6.Exit the program')
    print('********************')
    print('What do you waht to do?')

#Initialization of system,load the menber list
def begin():
    global itemlist
    #if os.path.exists('Information.txt') == True: #To judge whether the file exists
    listfile = open('Information.txt','rb')
    '''
    if len(listfile.read())!= 0: # To judge whether the file is empty
        listfile.seek(0)
    itemlist = p.load(listfile)'''
    listfile.close()

#Exitance of system,store the member list
def end():
    global itemlist
    listfile = open('Information.txt','w+')
    p.dump(itemlist,listfile)
    listfile.close()
    
#Insert an item into the member list
def insert():
    name = input('Enter the name:')
    age = int(input('Enter the age:'))
    gender = input('Enter the gender:')
    Chinese = float(input('Enter the Chinese score:'))
    Math = float(input('Enter the Math score:'))
    English = float(input('Enter the English score:'))
    item = Item(name,age,gender,Chinese,Math,English)
    global itemlist
    itemlist.append(item)
    print('Insert done!')

#Print an item
def output(item):

    #itemlist = p.load(listfile)
    print('%-10s%-5d%-7s%-12f%-12f%-12f%f' % (item.name,item.age,item.gender,item.Chinese,item.Math,item.English,(item.Chinese+item.Math+item.English)/3.0))

#Print all items
def display():
    global itemlist
    l = len(itemlist)
    print('name    age    gender    Chinese    Math    English    AVG(Score)')
    for i in range(0,l):
          output(itemlist[i])
    print('')
          

#Delete an item by name from member list
def delete():
    name = input('Enter the name what you want to delete:')
    global itemlist
    l = len(itemlist)
    for i in range(0,l):
        if (itemlist[i].name == name):
            itemlist.pop(i)
            break
#Update an item
def update(item):
    item.name = input('Enter the name:')
    age = int(input('Enter the age:'))
    gender = input('Enter the gender:')
    Chinese = float(input('Enter the Chinese score:'))
    Math = float(input('Enter the Math score:'))
    English = float(input('Enter the English score:'))

#Update an item's information ny name
def modify():
    name = input('Enter the name what you want to modify:')
    global itemlist
    l = len(itemlist)
    for i in range(0,l):
        if (itemlist[i].name == name):
            update(itemlist[i])
    print('Update done!')

#Sort all items by age
def sort():
    global itemlist
    itemlist.sort(key = lambda item:item.age)
    display()
    print('Sort done')

#Here are the Scripts
itemlist = [] #!!!!
begin()
while True:
    menu()
    choice = int (input())
    if choice == 1:
        insert()
    elif choice == 2:
        delete()
    elif choice == 3:
        modify()
    elif choice == 4:
        display()
    elif choice == 5:
        sort()
    elif choice == 6:
        exit()
    else:
        print('Your input is incorrect , system will exit.')
        break
end()
print('Good Bye!')
        

       代碼可以直接運行。

不吹不黑的說,Python做這個更簡單。代碼清晰,冗贅量小。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM