python编写自定义函数print_triangle(n)_12_Python语法示例(函数)

'''+--------------------------------+ # show_menu() 显示功能选择菜单

| 1) 添加学生信息 | # input_student()

| 2) 显示所有学生的信息 | # output_student() 可作为筛选的嵌套调用

| 3) 删除学生信息 | # delete_student()

| 4) 修改学生成绩 | # modify_student()

| 5) 按学生成绩高-低显示学生信息 | # desc_score() 4种排序并一个函数sort_student()

| 6) 按学生成绩低-高显示学生信息 | # asc_score()

| 7) 按学生年龄高-低显示学生信息 | # desc_age()

| 8) 按学生年龄低-高显示学生信息 | # asc_age()

| 9) 保存学生信息到文件(si.txt) | # write_file()

| 10) 从文件中读取数据(si.txt) | # read_file()

| q) 退出 | # quit()

+--------------------------------+'''

#显示功能选择菜单

defshow_menu(lst):#显示菜单

show_list =['+--------------------------------+','1) 添加学生信息','2) 显示所有学生的信息','3) 删除学生信息','4) 修改学生成绩','5) 按学生成绩高-低显示学生信息','6) 按学生成绩低-高显示学生信息','7) 按学生年龄高-低显示学生信息','8) 按学生年龄低-高显示学生信息','9) 保存学生信息到文件(si.txt)','10) 从文件中读取数据(si.txt)','q) 退出','+--------------------------------+']for row inshow_list:print(row)#选择菜单

menu_dic ={'1': input_student, #添加

'2': output_student, #显示

'3': delete_student, #删除

'4': modify_student, #修改

'5': sort_student, #排序

'6': sort_student,'7': sort_student,'8': sort_student,'9': write_file, #保存学生数据

'10': read_file, #读取学生数据

'q': quit #退出

}#选择功能

choice = input('>>>') or '-1'

if choice inmenu_dic:if choice == '6':

menu_dic[choice](lst, desc=False)elif choice == '7':

menu_dic[choice](lst, basis='age')elif choice == '8':

menu_dic[choice](lst, basis='age', desc=False)else:

menu_dic[choice](lst)else:print("输入有误,请从新选择")#1.添加学生信息

definput_student(lst):whileTrue:

name= input("请输入学生姓名:") or '-1'

if name == '-1': breakage= input("请输入学生年龄:")

score= input("请输入学生成绩:")

lst.append({'name': name,'age': age,'score': score

})#2.显示所有学生信息

defoutput_student(lst):defhead(length):

s= '+%s+%s+%s+' % ('-' * (length), '-' * 5, '-' * 7)print(s)def body(length, *args):

s= '|%s|%s|%s|' % (args[0].center(length), args[1].center(5), args[2].center(7))print(s)iflst:

length= max(map(lambda x: len(x['name']), lst)) + 2head(length)#开始表格线+------+-----+-------+

body(length, 'name', 'age', 'score') #抬头行| name | age | score |

head(length) #分割表格线+------+-----+-------+

for i inlst:

info= (i['name'], i['age'], i['score'])

body(length,*info) #内容

head(length) #结束表格线+------+-----+-------+

else:print("没有学生信息,请选择: 1)添加学生信息")#3.删除学生信息

defdelete_student(lst):

choice= input("请输入要删除学生的姓名:") or '-1'sign=Falsefor i, item inenumerate(lst):if choice == item['name']:

sign=Truebreak

else:print("你要删除的学生不存在")ifsign:

lst.pop(i)print("已删除学生:%s的信息" %choice)#修改学生成绩

defmodify_student(lst):

choice= input("请输入要修改学生的姓名:") or '-1'

for item inlst:if choice == item['name']:

temp_list=[item]

output_student(temp_list)

score= input("请输入新的成绩:") or '-1'item['score'] =scoreprint("已修改学生:%s的成绩" %choice)break

else:print("你要修改成绩的学生不存在")#排序-->按选择的条件排序列表后,作为实参传递给显示函数output_student(lst)

def sort_student(lst, basis='score', desc=True): #默认按年龄大到小

lst = sorted(lst, key=lambda dic: int(dic[basis]), reverse=desc)

output_student(lst)#保存学生信息

def write_file(lst, file="si.txt"):try:

f= open(file, "w")for i inlst:

s= "%s,%s,%s\n" % (i['name'], i['age'], i['score'])

f.write(s)

f.flush()

f.close()print("已成功保存学生信息到[%s]文件" %file)exceptIOError:print("文件打开失败")#读取学生信息

def read_file(lst, file="si.txt"):try:

f=open(file)whileTrue:

s=f.readline()if nots:

f.close()breaks=s.rstrip()

s= s.split(",")

dic= {'name': s[0], 'age': s[1], 'score': s[2]}

lst.append(dic)print("已成功从文件:[%s]读取到学生信息" %file)exceptIOError:print("文件打开失败")defmain():"""学生信息管理项目主逻辑入口"""

whileTrue:

show_menu(L)if __name__ == "__main__":

L=[]

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值