Python 基础 一些小练习(面向对象)

1 学生管理系统 

要求  能够储存 查询 修改 删除 学生信息(姓名、年龄、电话、住址等)

class StudentManagementSystem(object):
    student_info_lst = []

    @staticmethod
    def show_mean():
        """显示所有功能"""
        print("欢迎来到学生管理系统")
        print("新增学生请输入 [1]:")
        print("显示全部请输入 [2]:")
        print("查询学生请输入 [3]:")
        print("退出系统请输入 [0]:")

    def add_student(self):
        """新增学员功能"""
        print("--------新增学员功能------")
        name = input('请输入姓名:')
        age = input('请输入年龄:')
        gender = input('请输入性别:')
        phone = input('请输入手机号码:')
        # print(name,age,gender,phone)
        # 将用户输入的信息保存到一个字典中
        dic = {}
        dic['name'] = name
        dic['age'] = age
        dic['gender'] = gender
        dic['phone'] = phone
        # print(dic)
        # 注意  将用户输入的信息保存到全局变量student_info_lst
        # 列表中,让其他功能使用列表中的数据
        self.student_info_lst.append(dic)
        # print(student_info_lst)
        print("新增成功!!!!!")

    def show_all_student(self):
        print('--------显示所有的学员--------')
        if not self.student_info_lst:
            print('还没有学员,请添加学员')
            return

        # 显示学员的信息
        name = self.alignment('name', 15)
        age = self.alignment('age', 15)
        gender = self.alignment('gender', 15)
        phone = self.alignment('phone', 15)
        print(name, age, gender, phone)

        for dic in self.student_info_lst:
            # name = dic['name']
            # age = dic['age']
            # gender = dic['gender']
            # phone = dic['phone']
            # print('{}\t{}\t{}\t{}\t'.format(name, age, gender, phone))

            name = self.alignment(dic['name'], 15)
            age = self.alignment(dic['age'], 15)
            gender = self.alignment(dic['gender'], 15)
            phone = self.alignment(dic['phone'], 15)
            print(name, age, gender, phone)

    def alignment(self,s, space):  # 矫正
        """动态获取空格长度"""
        s1 = s.encode('gbk')
        length = len(s1)
        space = space - length

        s = s + ' ' * space
        return s

    def search_student(self):
        print('----------欢迎来到查询功能---------')
        if self.student_info_lst:
            name = input('请输入你想查询的学员姓名')
            for dic in self.student_info_lst:
                if dic['name'] == name:
                    print('name\tage\tgender\tphone')
                    name = dic['name']
                    age = dic['age']
                    gender = dic['gender']
                    phone = dic['phone']
                    print('{}\t{}\t{}\t{}\t'.format(name, age, gender, phone))
                    # 修改学员信息
                    # 需要将学员的信息传递到函数中
                    self.deal_student(dic)
                    break

            else:
                print('没有找到此学生')

        else:
            print('没有学员 请添加!!!!!')

    def deal_student(self,dic):
        """修改和删除学员信息"""
        action_str = input("【1】修改  【2】删除  【0】返回上一级菜单,请选择要执行的操作")
        if action_str == '1':
            name = self.modify_student(dic['name'], '请输入姓名:')
            age = self.modify_student(dic['age'], '请输入年龄:')
            gender = self.modify_student(dic['gender'], '请输入性别:')
            phone = self.modify_student(dic['phone'], '请输入电话:')
            dic['name'] = name
            dic['age'] = age
            dic['gender'] = gender
            dic['phone'] = phone

            print('-------修改成功----------')

        elif action_str == '2':
            self.student_info_lst.remove(dic)
            print('--------删除成功---------')

        elif action_str == '0':
            pass

    def modify_student(self,old_value, msg):
        new_value = input(msg)
        if new_value.strip():
            return new_value
        else:
            return old_value


while True:
    StudentManagementSystem.show_mean()
    sms = StudentManagementSystem()
    action_str = input("请输入你想选的操作")
    lst = ['0', '1', '2', '3']
    if action_str in lst:
        if action_str == "1":
            sms.add_student()
        elif action_str == "2":
            sms.show_all_student()
        elif action_str == "3":
            sms.search_student()
        elif action_str == "0":
            print("------欢迎下次管理-----")
            break
    else:
        print("-------sb 输入错了,请重新输入---------")

2  斐波那契数列的生成器方式

class Fibonacci():

    def __init__(self, all_num):
        self.all_num = all_num
        self.a = 1
        self.b = 1
        self.current_num = 0

    def __iter__(self):
        return self  # 返回自己  调用自己的__new函数

    def __next__(self):
        if self.all_num <= 2:
            self.current_num += 1
            if self.current_num == 3:
                raise StopIteration
            return self.a

        else:
            if self.current_num < self.all_num:
                ret = self.a
                self.a, self.b = self.b, self.a + self.b
                self.current_num+=1
                return ret
            else:
                raise StopIteration

for i in Fibonacci(10):
    print(i)

 

你可能感兴趣的:(python,新人学习Python必关注,零基础,小白萌新必看)