前言
今天利用Python基礎的知識來做一個學生資訊管理系統,界面大概是這樣的
**************************************************
歡迎使用【學生資訊管理系統】V1.0
請選擇你想要進行的操作
1. 新建學生資訊
2. 顯示全部資訊
3. 查詢學生資訊
4. 洗掉學生資訊
5. 修改學生資訊
0. 退出系統
**************************************************
知識點
- 基本的資料型別與結構
- 基本的邏輯控制陳述句
代碼
import json # students = [# {'name': '張三', 'math': 65, 'chinese': 65, 'english': 65, 'total': 195},# {'name': '李四', 'math': 65, 'chinese': 65, 'english': 65, 'total': 195},# ]with open('students.json', mode='r', encoding='utf-8') as f: text = f.read() students = json.loads(text) while True: print(info) # 從外部輸入用戶的操作 action = input('請選擇你想要的進行的操作:') # 增刪改查, 應該在那里面操作 if action == '1': print('1. 新建學生資訊') name = input('請輸入學生的名字:') math = int(input('請輸入學生的數學成績:')) chinese = int(input('請輸入學生的語文成績:')) english = int(input('請輸入學生的英語成績:')) total = math + chinese + english students.append({ 'name': name, 'math': math, 'chinese': chinese, 'english': english, 'total': total}) elif action == '2': # print('2. 顯示全部資訊') print('姓名\t數學\t語文\t英語\t總分') for student in students: print('{}\t{}\t\t{}\t\t{}\t\t{}'.format(*student.values())) elif action == '3': # print('3. 查詢學生資訊') name = input('請輸入你想查詢的學員名字:') for student in students: # for 回圈下面會查找三個 if student['name'] == name: print('姓名\t數學\t語文\t英語\t總分') print('{}\t{}\t\t{}\t\t{}\t\t{}'.format(*student.values())) break else: # 學員不存在只有一個 print(f'{name} 這個學員不存在') elif action == '4': print('4. 洗掉學生資訊') name = input('請輸入你想要的洗掉的學員姓名') for student in students: if student['name'] == name: # 把原本列印新的地方變成洗掉的 # del pop remove # students.remove(student) # students.pop(students.index(student)) del students[students.index(student)] break else: # 學員不存在只有一個 print(f'{name} 這個學員不存在') elif action == '5': # 首先找到學員,然后在修改學員的資訊 name = input('請輸入你想要的修改的學員姓名') print('(如果輸入為空,就不修改學生資訊)') for student in students: if student['name'] == name: # 如果不輸入內容,就不修改成績 math = input('請重新輸入學生的數學成績:') if math: math = int(math) student['math'] = math chinese = input('請重新輸入學生的語文成績:') if chinese: chinese = int(chinese) student['chinese'] = chinese english = input('請重新輸入學生的英語成績:') if english: english = int(english) student['english'] = english student['total'] = student['math'] + student['chinese'] + student['english'] break else: # 學員不存在只有一個 print(f'{name} 這個學員不存在') elif action == '0': print('0. 退出系統') with open('students.json', mode='w', encoding='utf-8') as f: f.write(json.dumps(students, ensure_ascii=False)) break else: print('你輸入的選項錯了,請重新在選擇')
最后運行代碼:
PS:如有需要Python學習資料的小伙伴可以加下方的群去找免費管理員領取
可以免費領取原始碼、專案實戰視頻、PDF檔案等
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/65144.html
標籤:Python
