递归实现
def age(num):
if num == 1:
return 10
return age(num-1) + 2
print(age(5)) 结果18
student = []
from menu import menu,user_choice,show_over
from Input import inf_input,stu_sort,inf_output,inf_index
def main():
while True:
menu()
choice = user_choice()
if choice == 0:
show_over()
if choice == 1:
inf_input(student,3)
if choice == 2:
inf_output(student)
if choice == 3:
stu_sort(student)
inf_index(student)
if choice == 4:
stu_sort(student)
inf_output(student)
if __name__ == '__main__':
main()
实现0-4函数功能文件 Input.py
def inf_input(student,num):
"""
:param student: 存放学生信息的列表
:param num: 学生的个数
:return: 无
"""
for i in range(num):
student2 = {}
student1 = input(f"请输⼊第{i+1}个学⽣的信息(按姓名、学号、数学成绩、计算机成绩输⼊,空格隔开):").split(" ")
student2['姓名'] = student1[0]
student2['学号'] = student1[1]
student2['数学成绩'] = float(student1[2])
student2['计算机成绩'] = float(student1[3])
student2['总成绩'] = float(student1[2]) + float(student1[3])
student2['平均成绩'] = (float(student1[2]) + float(student1[3])) / 2
student.append(student2)
def stu_sort(student):
"""
:param student: 学生信息列表
:return: 无
"""
student.sort(key=lambda student:student['总成绩'],reverse=True)
def inf_output(student):
"""
输出学生信息
:param student:
:return:
"""
fm1 = "{:^10}\t{:^10}\t{:<10}\t{:<10}\t{:<10}\t{:<10}"
fm2 = "{:^10}\t{:^10}\t{:^10}\t{:^14}\t{:^12}\t{:^14}"
f2 = fm1.format("姓名","学号","数学成绩","计算机成绩","总成绩","平均成绩")
print(f2)
for i in range(len(student)):
print(fm2.format(student[i]['姓名'],
student[i]['学号'],
student[i]['数学成绩'],
student[i]['计算机成绩'],
student[i]['总成绩'],
student[i]['平均成绩']))
def inf_index(student):
"""
按学号查找学生信息
:param student:
:return:
"""
fm1 = "{:^10}{:^10}\t{:^10}\t{:<10}\t{:<10}\t{:<10}\t{:<10}"
fm2 = "{:^10}\t{:^10}\t{:^10}\t{:^10}\t{:^14}\t{:^12}\t{:^14}"
ID = input("请输入你要查询的学号:")
for i in range(len(student)):
if ID == student[i]['学号']:
print(fm1.format("名次","姓名","学号","数学成绩","计算机成绩","总成绩","平均成绩"))
stu = fm2.format(i+1, student[i]['姓名'],
student[i]['学号'],
student[i]['数学成绩'],
student[i]['计算机成绩'],
student[i]['总成绩'],
student[i]['平均成绩'])
print("查询成功,该学生的信息为:")
print(stu)
break
else:
print("没有该学生的信息")
if __name__ == '__main__': # 只在本模块调用
student = []
inf_input(3)
用户界面 menu.py
def menu():
print("""
=====欢迎使用学生信息管理系统====
[1] 学生信息输入
[2] 学生信息输出
[3] 查找学生信息
[4] 成绩排序
[0] 退出
""")
def user_choice():
while True:
choice = int(input("请输入你的选择:"))
if choice < 0 or choice > 4:
print("请输入正确的选项")
else:
return choice
def show_over():
print("="*30)
print("\t\t感谢你的使用")
print("="*30)
exit(0)
执行结果