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做这个更简单。代码清晰,冗赘量小。

 

很想高飞,但我不能;不想天空,剩我一人。
原文地址:https://www.cnblogs.com/lixiansheng/p/6940438.html