1. 程式人生 > >修改學生資訊系統--實現新增選單和選擇選單操作功能

修改學生資訊系統--實現新增選單和選擇選單操作功能

4.修改之前的學生管理系統,實現新增選單和選擇選單操作功能

選單:

+-----------------------------------------------+
|  1)新增學生資訊                               |
|  2)檢視所有學生資訊                           |
|  3)修改學生的成績                             |
|  4)刪除學生的資訊                             |
|   q) 退出                                     |
+------------------------------------------------+
請選擇:1
 請輸入姓名:。。。
請選擇:3
請輸入修改學生的姓名:。。。
(每個功能都對應一個函式)
l = [{"name":"小豬","age":21,"score":90},{"name":"大白","age":13,"score":90}]
# 新增學生資訊
def add_student():
    while 1:
        name = input("請輸入學生姓名:")
        if not name:
            break
        age = int(input("請輸入學生年齡:"))
        score = int(input("請輸入學生成績:"))
        d = {}  # 建立一個新的字典
        d["name"] = name  # 值 對 鍵
        d["age"] = age
        d["score"] = score
        l.append(d)

# 檢視學生資訊
def output_student():
    print("+-------------+-------+----------+")
    print("|    name     |  age  |  score  |")
    print("+-------------+-------+----------+")
    for d in l:
        t = ((d["name"]).center(11),
             str(d["age"]).center(7),
             str(d["score"]).center(10))
        line = "|%s|%s|%s|" % t  # t是元祖
        print(line)
        print("+-------------+-------+----------+")
# 修改學生資訊
def alter_student():
    while 1:
        i=input("請輸入要修改的學生姓名:")
        if not i:
            break
        for x in l:#遍歷l中的字典
            if x["name"]==i:
                x["age"] = int(input("請輸入新的學生年齡:"))
                x["score"] = int(input("請輸入新的學生成績:"))
                print("已成功修改",i,"的資訊!")
                break
        else:
            print("沒有找到姓名為",i,"的學生!")
# 刪除學生資訊
def delete_student():
    while 1:
        i=input("請輸入要刪除的學生姓名:")
        if not i:
            break
        for a in l:#遍歷l中的字典
           if a["name"]==i:
                    l.pop(l.index(a))
                    print("已成功刪除名為",i,"的學生資訊!")
                    break
        else:
            print("沒有找到名為",i,"的學生!")
def show_menu():
        print("+------------------------+")
        print("| 1)新增學生資訊          |")
        print("| 2)檢視所有學生資訊      |")
        print("| 3)修改學生資訊          |")
        print("| 4)刪除學生資訊          |")
        print("| q)退出                  |")
        print("+-------------------------+")
# 主函式
def main():
    show_menu()
    while 1:
        n=input("請輸入要操作的序號:")
        if n=="1":
            add_student()
        if n=="2":
            output_student()
        if n=="3":
            alter_student()
        if n=="4":
            delete_student()
        if n=="q"or n=="Q":
            exit()
main()