使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话
all\_students = [
{'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'},
{'name':'stu2', 'age': 29, 'score':90, 'tel':'211222'},
{'name':'stu3', 'age': 12, 'score':67, 'tel':'521114'},
{'name':'stu4', 'age': 30, 'score':45, 'tel':'900012'},
]
1.添加学生:输入学生信息,将输入的学生的信息保存到all_students中
例如输入:
姓名: 小明
年龄: 20
成绩: 100
电话: 111922
那么就在all\_students中添加{'name':'小明', 'age': 20, 'score': 100, 'tel':'111922'}
while True:
for i in range(4):
name = input('姓名: ')
all_students[i]['name'] = name
age = input('年龄: ')
all_students[i]['age'] = age
score = input('成绩: ')
all_students[i]['score'] = score
tel = input('电话: ')
all_students[i]['tel'] = tel
print('-'*30)
print(all_students)
break
2.按姓名查看学生信息:
例如输入:
姓名: stu1 就打印:'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'
while True:
name = input('请输入要查询的姓名:')
if name == 'end':
break
for data in all_students:
if name == data['name']:
for key, value in data.items():
print(key, value)
continue
print('未找到学生,请重新输入。')
3.求所有学生的平均成绩和平均年龄
sum = 0
for data in all_students:
sum += data['age']
print('平均年龄:',sum / 4)
sum = 0
for data in all_students:
sum += data['score']
print('平均成绩:',sum / 4)
4.删除班级中年龄小于18岁的学生
for data in all_students:
if data['age'] < 18:
for key in data:
data[key] = ''
print(all_students)
提高作业
all_students = []
mainmenu = {1: '添加学生', 2:'查看学生', 3: '修改学生信息', 4:'删除学生', 5: '返回'}
submenu_2 = {1: '查看所有学生', 2: '按姓名查找', 3: '按学号查找', 4: '返回'}
submenu_3 = {1: '按姓名修改', 2: '按学号修改', 3: '返回'}
submenu_4 = {1: '删除所有学生', 2: '按姓名删除', 3: '按学号删除', 4: '返回'}
def getNum(text : str) -> str:
while True:
num = input(text)
if len(num) == 4 and num.isdigit():
return num
else:
print('错误,学号为4位数字!请重新输入')
def getName(text : str) -> str:
name = input(text)
return name
def getTel(text : str) -> str:
while True:
tel = input(text)
if len(tel) == 11 and tel.isdigit():
return tel
else:
print('错误,电话号码为11位数字!请重新输入')
def getAge(text : str) -> str:
while True:
age = input(text)
if age.isdigit() and 1 <= len(age) <= 2:
return age
else:
print('年龄不合规范!请输入正确的年龄。')
def matchDataAndOperate(data, value, tag, func):
if all_students == []:
print('=' * 56)
print('目前没有学生!请添加学生。')
print('=' * 56)
elif tag == '':
for data_students in data:
func(data_students, tag, value)
elif tag != '':
for data_students in data:
if value == data_students[tag]:
func(data_students, tag, value)
break
else:
print('没有找到该学生')
def appendStudentData(data, all_students):
all_students.sort(key = lambda x: x['num'])
name = getName('请输入要修改的学生姓名:')
if all_students == []:
num = '0001'
else:
while True:
for data_students in all_students:
if all_students.index(data_students) + 1 < int(data_students['num'][-1]):
num = str(int(data_students['num'][-1]) - int(all_students.index(data_students)) - 1).rjust(4, '0')
break
else:
num = str(len(all_students) + 1).rjust(4, '0')
break
age = getAge('请输入要修改的学生年龄:')
tel = getTel('请输入要修改的学生电话:')
data['name'] = name
data['num'] = num
data['tel'] = tel
data['age'] = age
print('添加完成')
showStudentData(data)
return data
def changeStudentData(data, tag = None, value = None):
if tag == 'num':
print('修改学号:%s同学的信息' % value)
elif tag == 'name':
print('修改姓名:%s同学的信息' % value)
ifchangeage = input('是否更改年龄?[y/n]')
if ifchangeage == 'y':
age = getAge('请输入修改后的学生年龄:')
elif ifchangeage == 'n':
age = data['age']
ifchangetel = input('是否更改电话号码?[y/n]')
if ifchangetel == 'y':
tel = getTel('请输入修改后的学生电话号码:')
elif ifchangetel == 'n':
tel = data['tel']
data['tel'] = tel
data['age'] = age
print('修改完成')
showStudentData(data)
def showStudentData(data, tag = None, value = None):
print('学生%s的信息如下:' % data['name'])
print(' ')
print('学号:', data['num'].ljust(5, ' '), end = '')
print('学生:', data['name'].ljust(8, ' '), end = '')
print('年龄:', data['age'].ljust(5, ' '), end = '')
print('电话:', data['tel'].ljust(11, ' '), end = '')
print('')
print('=' * 56)
def delStudentData(data, tag = None, value = None):
all_students.remove(data)
print('删除成功')
def mainMenuReturnChoice(mainmenu) -> str:
print('=' * 36)
print(chr(0x1f338) * 2, '欢迎:')
print('')
print('')
for key, value in mainmenu.items():
print(chr(0x1f537) * 2, '%d.\t%s' % (key, value))
print('')
print('=' * 36)
choice = input('请选择(1-5):')
return choice
def subMenuReturnChoice(submenu) -> str:
for key, value in submenu.items():
print(key,'. ',value)
choice = input('请选择:')
print(' ')
return choice
def choiceExitorNot(funcname) -> str:
print('1. 继续%s\n2. 返回主菜单' % funcname)
cmd = input('请选择(1-2): ')
return cmd
def endTips():
print('=' * 36)
print('请根据提示进行选择。')
print('=' * 36)
while True:
choice = mainMenuReturnChoice(mainmenu)
if choice == '1':
while True:
data_students = {'num': '','name':'', 'age': '', 'tel':''}
data = appendStudentData(data_students, all_students)
all_students.append(data)
cmd = choiceExitorNot('添加')
if cmd == '1':
continue
else:
break
elif choice == '2':
all_students.sort(key = lambda x: x['num'])
while True:
choice_2 = subMenuReturnChoice(submenu_2)
if choice_2 == '1':
matchDataAndOperate(all_students, '', '',showStudentData)
elif choice_2 == '2':
name = getName('请输入要查找的学生姓名:')
matchDataAndOperate(all_students, name, 'name', showStudentData)
elif choice_2 == '3':
num = getNum('请输入要查询的学生学号:')
print(' ')
matchDataAndOperate(all_students, num, 'num', showStudentData)
elif choice_2 == '4':
break
else:
endTips()
continue
elif choice == '3':
while True:
choice_3 = subMenuReturnChoice(submenu_3)
if choice_3 == '1':
name = getName('请输入要修改的学生姓名:')
print(' ')
matchDataAndOperate(all_students, name, 'name', changeStudentData)
cmd = choiceExitorNot('修改')
if cmd == '1':
continue
else:
break
elif choice_3 == '2':
num = getNum('请输入要修改的学生学号:')
print(' ')
matchDataAndOperate(all_students, num, 'num', changeStudentData)
cmd = choiceExitorNot('修改')
if cmd == '1':
continue
else:
break
elif choice_3 == '3':
break
else:
endTips()
continue
elif choice == '4':
while True:
choice_4 = subMenuReturnChoice(submenu_4)
if choice_4 == '1':
for data in all_students[:]:
delStudentData(data)
print('返回主菜单。')
break
elif choice_4 == '2':
name = getName('请输入要删除的学生姓名:')
print(' ')
matchDataAndOperate(all_students, name, 'name', delStudentData)
cmd = choiceExitorNot('删除')
if cmd == '1':
continue
else:
break
elif choice_4 == '3':
num = getNum('请输入要删除的学生学号:')
print(' ')
matchDataAndOperate(all_students, num, 'num', delStudentData)
cmd = choiceExitorNot('删除')
if cmd == '1':
continue
else:
break
elif choice_4 == '4':
break
else:
endTips()
continue
elif choice == '5':
cmd = choiceExitorNot('退出')
if cmd == '1':
print('退出')
break
else:
continue
else:
endTips()
continue
网友评论