python学生情報管理
2896 ワード
#record student infomation
def input_student(L):
while True:
name = input("please input student name:")
if not name:
return L
age = input("please input age:")
score = input("input score:")
stu = {"name":name, "age":age,"score":score}
L.append(stu)
return L
# print student information
def output_student(lst):
s_frame = '+'+'-'*20+'+'+'-'*20+'+'+'-'*20+'+'
s_title = '|{:^20}|{:^20}|{:^20}|'.format('NAME','AGE','SCORE')
print(s_frame)
print(s_title)
print(s_frame)
for I in lst:
s_item = '|{:^20}|{:^20}|{:^20}|'.format(I['name'],I['age'],I['score'])
print(s_item)
print(s_frame)
return
#
def delete_menu(L):
s = input('delete student name:')
for stu in L:
if stu['name'] == s:
L.remove(stu)
return L
#
def modify_score(L):
print("modify student score.")
name = input('stu name:')
score = int(input('stu score:'))
for d in L:
if d['name'] == name:
d['score'] = score
return L
def sort_high_low(L):
print(" :")
return sorted(L, key = lambda d: int(d['score']), reverse = True)
def sort_low_high(L):
print(" ;")
return sorted(L, key = lambda d: d['score'], reverse = False)
def sort_age_HL(L):
print(" ")
return sorted(L, key = lambda d: d['age'], reverse = True)
def sort_age_LH(L):
print(" ")
return sorted(L, key = lambda d: d['age'], reverse = False)
# Operation Menu
def menu_stu():
L = []
s_menu = '''
1)
2)
3)
4)
5) -
6) -
7) -
8) -
q)
'''
while True:
print(s_menu)
choice = input(">>>")
if choice == '1':
L = input_student(L)
elif choice == '2':
output_student(L)
elif choice == '3':
L = delete_menu(L)
elif choice == '4':
L = modify_score(L)
elif choice == '5':
L = sort_high_low(L)
elif choice == '6':
L = sort_low_high(L)
elif choice == '7':
L = sort_age_HL(L)
elif choice == '8':
L = sort_age_LH(L)
elif choice == 'q':
return
else:
print("input error!!!")
#
def main():
menu_stu()
main()