class StudentManger(object):
student_info_lst = [] # 保存所有學(xué)生的信息
@staticmethod
def show_menu():
'''顯示所有功能'''
print('歡迎來到學(xué)生管理系統(tǒng)')
print('新增學(xué)員請按[1]')
print('顯示全部請按[2]')
print('查詢學(xué)員請按[3]')
print('退出系統(tǒng)請按[0]')
def add_student(self):
'''這是新增學(xué)員的功能'''
print('-------新增學(xué)員功能-------')
name = input('請輸入姓名:')
age = input('請輸入年齡:')
gender = input('請輸入性別:')
phone = input('請輸入手機(jī):')
# print(name,age,gender,phone)
# 將用戶輸入的信息保存到一個字典中。
dic = {}
dic['name'] = name
dic['age'] = age
dic['gender'] = gender
dic['phone'] = phone
# print(dic)
# 注意:將用戶數(shù)的信息保存到全局變量student_info_lst
# 列表中,讓其他功能使用列表中的數(shù)據(jù)。
self.student_info_lst.append(dic)
# print(student_info_lst)
print('添加成功了...')
def alignment(self,s, space):
'''輸出對齊'''
s1 = s.encode('gbk')
space = space - len(s1)
s = s + ' ' * space
return s
def show_all_student(self):
print('-------顯示所有的學(xué)員功能-------')
if not self.student_info_lst:
print('還沒有學(xué)員呢,請?zhí)砑訉W(xué)員')
return
# 顯示學(xué)員的信息
name = self.alignment('name', 15)
age = self.alignment('age', 15)
gender = self.alignment('gender', 15)
phone = self.alignment('phone', 15)
print(name, age, gender, phone)
for dic in self.student_info_lst:
name = dic['name']
age = dic['age']
gender = dic['gender']
phone = dic['phone']
name = self.alignment(name, 15)
age = self.alignment(age, 15)
gender = self.alignment(gender, 15)
phone = self.alignment(phone, 15)
print(name, age, gender, phone)
def modify_student_info(self,old_value, msg):
'''處理修改用戶名時不輸入問題'''
new_name = input(msg)
if new_name.strip():
return new_name
else:
return old_value
def deal_student(self,dic):
action_str = input("【1】修改 【2】刪除 【0】返回上一級菜單,請選擇要執(zhí)行的操作:")
if action_str == '1':
# 第一版修改
# dic['name'] = input('請輸入姓名:')
# dic['age'] = input('請輸入年齡:')
# dic['gender'] = input('請輸入性別:')
# dic['phone'] = input('請輸入電話:')
# 第二版--輸入空格后能繼續(xù)運(yùn)行。
dic['name'] = self.modify_student_info(dic['name'], '請輸入姓名:')
dic['age'] = self.modify_student_info(dic['age'], '請輸入年齡:')
dic['gender'] = self.modify_student_info(dic['gender'], '請輸入性別:')
dic['phone'] = self.modify_student_info(dic['phone'], '請輸入電話:')
print('修改成功!??!')
elif action_str == '2':
self.student_info_lst.remove(dic)
print('刪除成功了!!!')
elif action_str == '0':
pass
def search_student_by_name(self):
'''搜索學(xué)生'''
print('-------歡迎來到查詢學(xué)員功能-------')
if self.student_info_lst:
name = input('請輸入要查詢學(xué)員的姓名:')
for dic in self.student_info_lst:
if dic['name'] == name:
name = self.alignment('name', 15)
age = self.alignment('age', 15)
gender = self.alignment('gender', 15)
phone = self.alignment('phone', 15)
print(name, age, gender, phone)
name = dic['name']
age = dic['age']
gender = dic['gender']
phone = dic['phone']
name = self.alignment(name, 15)
age = self.alignment(age, 15)
gender = self.alignment(gender, 15)
phone = self.alignment(phone, 15)
print(name, age, gender, phone)
# 處理學(xué)生信息
self.deal_student(dic)
break
else:
print('not have such student')
else:
print('not have student please add student')
while True:
StudentManger.show_menu()
student_manager=StudentManger()
action_str = input('請輸入你要選擇數(shù)字:')
lst = ['0', '1', '2', '3']
if action_str in lst:
if action_str == '1':
student_manager.add_student()
elif action_str == '2':
student_manager.show_all_student()
elif action_str == '3':
student_manager.search_student_by_name()
elif action_str == '0':
print('三位慢走,歡迎下次光臨')
break
else:
print('-------sb 輸出錯了請重新輸入...-------')