Python之基础实战项目

目录

  • 前言
    • 项目代码
  • 后言

前言

我们在之前的学习内容中就可以大概的了解了python的一个基础用法。
所以在之前的python【一】,python【二】,python【三】的基础上我们就对其进行一个整合。

let`go,代码展开。

项目代码

students = [{"name": '张三', "age": 12}, {"name": '李四', "age": 12}]


def selStu():
    """
    查询所有
    :return:
    """
    for stu in students:
        print(f"学生的姓名为:{stu['name']},学生的年龄为:{stu['age']}")



def selStuIndex(index):
    """
    查询单个
    :param index: 当个名字
    :return:
    """
    for stu in students:
        if stu['name'] == index:
            print(f"学生的姓名为:{stu['name']},学生的年龄为:{stu['age']}")

def addStu(name,age):
    """
    增加一个学生
    :param name: 增加名
    :param age: 增加年龄
    :return:
    """
    stu1 = {"name": name, "age": age}
    students.append(stu1)

def editStu(index,newname,newage):
    """
    修改学生
    :param index:  修改id
    :param newname: 修改后名
    :param newage: 修改后名
    :return:
    """
    stunew = {"name": newname, "age": newage}
    students[index] = stunew

while True:
    a = input("请输入你要进行的操作,\n1,查询学生\n2,增加学生\n3,修改学生\n4,删除学生")
    if '1' == a:
        while True:
            select = input("请输入你要进行的操作:\n1,查询所有\n2,查询单个\n3,返回上一级")
            if '1' == select:
                print("============查询学生=============")
                selStu()
            elif '2' == select:
                print("============查询单个学生=============")
                namesel = input("请输入要查询的单个学生")
                selStuIndex(namesel)
            else:
                print("成功返回上一级")
                break

    elif '2' == a:

        while True:
            select = input("请输入你要进行的操作:\n1,增加学生\n2,返回上一级")
            if '1' == select:
                print("==================增加学生==============")
                name1 = input("请输入要增加的学生姓名")
                age1 = input("请输入要增加的学生年龄")
                addStu(name1,age1)
                print("============成功增加================")
            else:
                print("成功返回上一级")
                break
    elif '3' == a:
        while True:
            select = input("请输入你要进行的操作:\n1,修改学生\n2,返回上一级")
            if '1' == select:
                print("==================修改学生==============")
                nameold = input("请输入要修改的学生姓名")
                oldindex = 0
                b = False
                for stu in students:
                    if stu['name'] == nameold:
                        b = True
                        oldindex = students.index(stu)
                if b:
                    print("==================找到了==============")
                    namenew = input("请输入要修改后的学生姓名:")
                    agenew = input("请输入要修改后的学生年龄:")
                    editStu(oldindex,namenew,agenew)
                    print("============成功增加================")
                else:
                    print("==================没有找到你输入的学生信息==============")

            else:
                print("成功返回上一级")
                break

    elif '4' == a:
        while True:
            select = input("请输入你要进行的操作:\n1,删除学生\n2,返回上一级")
            if '1' == select:
                print("==================删除学生==============")
                namedel = input("请输入要删除的学生姓名")
                delindex = 0
                b = False
                for stu in students:
                    if stu['name'] == namedel:
                        b = True
                        delindex = students.index(stu)
                if b:
                    print("==================找到了==============")
                    students.pop(delindex)
                    print("============成功删除================")
                else:
                    print("==================没有找到你输入的学生信息==============")

            else:
                print("成功返回上一级")
                break

    else:
        print("关闭了,拜拜")
        break

后言

此项目copy即可用,额,差不多的知识点都用到了ヽ(ー_ー)ノ

你可能感兴趣的:(Python)