python 超简易学生管理系统(未封装函数) 图书管理系统(已封装函数)

小白向,无函数,无数据库。

后面有时间会加上这些。

根目录下info.txt保存数据。

学生管理系统(未封装函数)

'''
	
	
    学生管理系统

    requests.session模拟登录
    selenium??
    tk+sqlite
    ctrl+d快速复制一行
    alt 点住光标批量修改
    mysql+sqlalchemy
'''

str_welcome = '''
    print('1新建')
    print('2显示')
    print('3查询')
    print('4删除')
    print('5修改')
    print('0退出')

'''
import json

with open('info.txt', mode='r', encoding='utf-8') as f:
    # 读取完后是字符串
    text = f.read()
#     将字符串变为python可用对象
student_list = eval(text)
# # 列表里用字典
# student_list = [
#     {'name':'zhang' , 'chinese':'12' , 'math':'23' , 'total':'1331'},
#     {'name':'liu' , 'chinese':'12' , 'math':'23' , 'total':'1331'},
#     {'name':'hao' , 'chinese':'12' , 'math':'23' , 'total':'1331'},
#     {'name':'chow' , 'chinese':'12' , 'math':'23' , 'total':'1331'},
#
# ]
while True:
    print(str_welcome)
    action = input('输入功能:')
    if action == '1':
        #列表里用字int(典
        d['name'] = int(input('请输入姓名'))
        d['chinese'] = int(input('请输入yuwen'))
        d['math'] = int(input('math '))
        d['total'] = d['chinese'] + d['math']
        student_list.append(d)
        print('新建')
    elif action == '2':
        print('显示')
        print('姓名\t\t语文\t\t数学\t\t英语\t\t总分')
        for student in student_list:
            print(f'{student["name"]}\t\t{student["chinese"]}\t{student["math"]}\t{student["total"]}')
           # print(*student.values()) #解包

    elif action == '3':
         name = input('输入姓名查询:')
         for student in student_list:
             if student['name'] == name:
                 print('姓名\t\t语文\t\t数学\t\t英语\t\t总分')
                 print(f'{student["name"]}\t\t{student["chinese"]}\t\t{student["math"]}\t\t{student["total"]}')
                 break
         else:
             print("None\n")
    elif action == '4':
        name = input('输入姓名查询:')
        for student in student_list:
            if student['name'] == name:
                print('已删除')
                # 删除列表元素    pop索引 remove元素 del索引
                student_list.remove(student)
                # student_list.pop(student_list.index(student))
                break
        else:
            print("None\n")
        print('删除')
    elif action == '5':
        name = input('输入姓名查询:')
        for student in student_list:
            if student['name'] == name:
                student['name'] = int(input('请输入姓名'))
                student['chinese'] = int(input('请输入yuwen'))
                student['math'] = int(input('math '))
                student['total'] = student['chinese'] + student['math']
                student_list.append(d)
                break
        else:
            print("None\n")
        print('修改')
    elif action == '0':
        print('退出')
        #退出保存
        with open('info.txt' , mode = 'w' , encoding='utf-8') as f:
            f.write(str(student_list))
        break
        # exit
    else:
        print('error')
        break


图书管理系统(已封装函数)
info.txt保存文件
文件格式

[{'name': 'book_0', 'price': '48', 'publish': 'publish_0', 'star': '99.8'}, {'name': 'book_1', 'price': '42', 'publish': 'publish_1', 'star': '98.8'}, {'name': 'book_2', 'price': '48', 'publish': 'publish_2', 'star': '95.9'}, {'name': 'book_3', 'price': '43', 'publish': 'publish_3', 'star': '94.1'}, {'name': 'book_4', 'price': '37', 'publish': 'publish_4', 'star': '90.9'}, {'name': 'book_5', 'price': '43', 'publish': 'publish_5', 'star': '94.3'}, {'name': 'book_6', 'price': '40', 'publish': 'publish_6', 'star': '94.8'}, {'name': 'book_7', 'price': '38', 'publish': 'publish_7', 'star': '94.9'}, {'name': 'book_8', 'price': '46', 'publish': 'publish_8', 'star': '90.1'}, {'name': 'book_9', 'price': '43', 'publish': 'publish_9', 'star': '92.5'}, {'name': '123', 'price': '123', 'publish': '123', 'star': '123'}]
# 排序函数 param参数决定按好评还是价格排序
def sort(booke_list, param):
    sort_list = []
    set = ()
    for n in range(0, len(booke_list)):
        set = (n, float(booke_list[n][param]))
        sort_list.append(set)
    sort_list = sorted(sort_list, key=lambda sort: sort[1]) #价格排序
    # print(sort_list)
    print('书名\t\t\t价格\t\t出版社\t\t好评度')
    for m in range(0, len(booke_list)):
        book = booke_list[sort_list[m][0]]
        print(f'{book["name"]}\t\t{book["price"]}\t\t\t{book["publish"]}\t{book["star"]}')
#新建图书
def book_mkdir():
    # 列表里用字int(典
    name = input('请输入书名')
    price = input('请输入价格:')
    publish = input('请输入出版社:')
    star = input('请输入好评:')
    add = {
        'name': name,
        'price': price,
        'publish': publish,
        'star': star
    }
    print("add success")
    return add
#展示图书
def book_showdir(booke_list):
    print('显示')
    print('书名\t\t\t价格\t\t出版社\t\t好评度')
    for booke in booke_list:
        # f 能在这里代表转义字典的括号
        print(f'{booke["name"]}\t\t{booke["price"]}\t\t\t{booke["publish"]}\t{booke["star"]}')
    # print(*booke.values()) #解包
#查找书
def book_match(booke_list):
    name = input('输入书名查询:')
    for booke in booke_list:
        if booke['name'] == name:
            print('书名\t\t\t价格\t\t出版社\t\t好评度')
            print(f'{booke["name"]}\t\t{booke["price"]}\t\t{booke["publish"]}\t\t{booke["star"]}')
            break
    else:
        print("None\n")
#删除
def book_del(booke_list):
    name = input('输入书名删除:')
    for booke in booke_list:
        if booke['name'] == name:
            print('已删除')
            # 删除列表元素    pop索引 remove元素 del索引
            booke_list.remove(booke)
            # booke_list.pop(booke_list.index(booke))
            break
    else:
        print("None\n")
    print('已删除')
    return booke_list
#修改
def book_modify(booke_list):
    name = input('输入书名修改:')
    for booke in booke_list:
        if booke['name'] == name:
            booke['name'] = int(input('请输入新的书名:'))
            booke['price'] = int(input('请输入新的价格:'))
            booke['publish'] = int(input('输入新的出版社'))
            booke['star'] = booke['price'] + booke['publish']
            # booke_list.append(d)
            print('已修改')
            break
    else:
        print("None\n")
    return booke_list
#保存
def book_save(booke_list):
    print('退出')
    # 退出保存
    with open('info.txt', mode='w', encoding='utf-8') as f:
        f.write(str(booke_list))

if __name__ == '__main__':
    str_welcome = '''
        print('1---------新建')
        print('2---------显示')
        print('3---------查询')
        print('4---------删除')
        print('5---------修改')
        print('6---------排序')
        print('0---------退出')
    '''
    with open('info.txt', mode='r', encoding='utf-8') as f:
        # 读取完后是字符串
        text = f.read()
    #     将字符串变为python可用对象
    booke_list = eval(text)
    # # 列表里用字典
    # booke_list = [
    #     {'name':'zhang' , 'price':'12' , 'publish':'23' , 'star':'1331'},
    #     {'name':'liu' , 'price':'12' , 'publish':'23' , 'star':'1331'},
    # ]
    while True:
        print(str_welcome)
        action = input('输入功能:')
        # print('1新建')
        if action == '1':
            booke_list.append(book_mkdir())
            next = input("输入任意值继续:")
        elif action == '2':
            book_showdir(booke_list)
            next = input("输入任意值继续:")
        elif action == '3':
            book_match(booke_list)
            next = input("输入任意值继续:")
        elif action == '4':
            booke_list = book_del(booke_list)
            next = input("输入任意值继续:")
        elif action == '5':
            booke_list = book_modify(booke_list)
            next = input("输入任意值继续:")
        elif action == '6':
            name = input('7  按价格排序\n8  按好评排序\n:')
            if name == '7':
                param = 'price'
                sort(booke_list, param)
            elif name == '8':
                param = 'star'
                sort(booke_list, param)
            next = input("输入任意值继续:")
        elif action == '0':
            book_save(booke_list)
            break
        else:
            print('error')
            break
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值