day_12作业

2018-07-31  本文已影响0人  我是一只菜鳥

作业1


"""
1.声明一个电脑类:  
属性:品牌、颜色、内存大小  
方法:打游戏、写代码、看视频  
"""
class Computer:
    def __init__(self, brand='', color='',memory=''):
        self.brand = brand
        self.color = color
        self.memory = memory
    @staticmethod
    def play_game():
        print('打游戏')

    @staticmethod
    def write_code():
        print('写代码')

    @staticmethod
    def watch_video():
        print('看视频')

if __name__ == '__main__':
    """
    a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性  
    b.通过attr相关方法去获取、修改、添加和删除它的属性  
    """
    comp1 = Computer('Apple', 'black', '16GB')

    print(comp1.brand, comp1.color, comp1.memory)
    print('==',getattr(comp1, 'brand'),getattr(comp1, 'color'),getattr(comp1, 'memory'))

    comp1.memory = '8GB'
    print(comp1.memory)
    setattr(comp1, 'memory', '4GB')
    print('==', comp1.memory)

    comp1.size = '15英寸'
    print(comp1.size)
    setattr(comp1, 'weight', '1kg')
    print('==', comp1.weight)

    del comp1.size
    # print(comp1.size)
    delattr(comp1, 'weight')
    # print(comp1.weight)

>>>

Apple black 16GB
== Apple black 16GB
8GB
== 4GB
15英寸
== 1kg

作业2

"""
2.声明一个人的类和狗的类:  
狗的属性:名字、颜色、年龄  狗的方法:叫唤  
人的属性:名字、年龄、狗   人的方法:遛狗  
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
"""

class Dog:
    """狗类"""
    def __init__(self, name='', color='', age=0):
        self.name = name
        self.color = color
        self.age = age


    def call(self):
        print('%s叫唤' % self.name)

class Person:
    """人类"""
    def __init__(self, name='', age=0, dog=[]):
        self.name = name
        self.age = age
        self.dog = dog

    def walk_dog(self):
        print('%s正在遛%s' % (self.name, self.dog.name))

if __name__ == '__main__':
    dog1 = Dog('大黄', 'yellow', 2)
    p1 = Person('小明', 18, dog1)
    p1.walk_dog()

>>>

小明正在遛大黄

作业3

"""
3.声明一个矩形类:  
属性:长、宽   方法:计算周长和面积  
a.创建不同的矩形,并且打印其周长和面积   
"""
class Rect:
    def __init__(self, length=0, width=0):
        self.length = length
        self.width = width

    def perimeter(self):
        return 2 * (self.length + self.width)

    def acre(self):
        return self.length * self.width

if __name__=='__main__':
    r1 = Rect(10, 5)
    print('r1的周长为:%d, 面积为:%d' % (r1.perimeter(), r1.acre()))

    r2 = Rect(20, 7)
    print('r2的周长为:%d, 面积为:%d' % (r2.perimeter(), r2.acre()))

    r3 = Rect(11, 11)
    print('r3的周长为:%d, 面积为:%d' % (r3.perimeter(), r3.acre()))

>>>

r1的周长为:30, 面积为:50
r2的周长为:54, 面积为:140
r3的周长为:44, 面积为:121

作业4

"""
4.创建一个学生类:  
属性:姓名,年龄,学号   方法:答到,展示学生信息
创建一个班级类:  
属性:学生,班级名   方法:添加学生,删除学生,点名  
"""
class Student:
    def __init__(self, name='', age=0, id=''):
        self.name = name
        self.age = age
        self.id = id

    def reach(self):
        print('%s:到' % self.name)

    @classmethod
    def show_info(cls):
        print(cls.name, cls.age, cls.id)

    def __str__(self):
        return 'name:%s, age:%d, id:%s' % (self.name, self.age, self.id)


class Class:
    def __init__(self, name='', students=[]):
        self.name = name
        self.students = students

    def add_student(self):
        print('添加学生')
        name = input('name:')
        age = int(input('age:'))
        id = input('id:')
        stu = Student(name, age, id)
        self.students.append(stu)

    def del_student(self):
        print('删除学生')
        name = input('name')
        flag = True
        index_list = []
        for index in range(0, len(self.students)):
            if name == self.students[index].name:
                flag = False
                print(index, self.students[index])
                index_list.append(index)
        if flag:
            print('没有找到')
        else:
            del_index = int(input('请输入要删除学生的下标:'))
            if del_index in index_list:
                del self.students[del_index]
                print('删除成功')
            else:
                print('下标输入错误')

    def roll_call(self):
        name = input('请输入要点名的名字:')
        for item in self.students:
            if name == item.name:
                print(item)
                item.reach()

if __name__=='__main__':
    # stu1 = Student('xxx', 18, '123')
    cla1 = Class('py1805')
    print(type(cla1.students))
    print(cla1.name, cla1.students)

    cla1.add_student()
    print(cla1.name, cla1.students)

    cla1.add_student()
    print(cla1.name, cla1.students)

    cla1.del_student()
    print(cla1.name, cla1.students)

    cla1.roll_call()

>>>

<class 'list'>
py1805 []
添加学生
name:aa
age:1
id:1
py1805 [<__main__.Student object at 0x021D6430>]
添加学生
name:bb
age:2
id:2
py1805 [<__main__.Student object at 0x021D6430>, <__main__.Student object at 0x021D6470>]
删除学生
namebb
1 name:bb, age:2, id:2
请输入要删除学生的下标:1
删除成功
py1805 [<__main__.Student object at 0x021D6430>]
请输入要点名的名字:aa
name:aa, age:1, id:1
aa:到

作业5

"""
5.写一个类,封装所有和数学运算相关的功能(包含常用功能和常用值,例如:pi,e等)
"""
import math

class MathOperation:
    pi = math.pi
    e = math.e
    def add(self, *number):
        sum = 0
        for item in number:
            sum += item
        return sum
    def sub(self, *number):
        dif =  number[0]
        for index in range(1, len(number)):
            dif -= number[index]
        return dif
    def mul(self, *number):
        mul1 = number[0]
        for index in range(1, len(number)):
            mul1 *= number[index]
        return mul1

if __name__=='__main__':
    num1 = [20, 5, 100]
    oper1 = MathOperation()
    print(oper1.pi, oper1.e)

    print(oper1.add(*num1))
    print(oper1.sub(*num1))
    print(oper1.mul(*num1))

>>>

3.141592653589793 2.718281828459045
125
-85
10000

作业6

"""
6.写一个班级类,属性:班级名、学生;功能:添加学生、删除学生、根据姓名查看学生信息,展示班级的所有学生信息
"""

class Class:
    def __init__(self, name='', students=[]):
        self.name = name
        self.students = students

    def add_student(self):
        print('添加学生')
        num = int(input('请输入添加学生人数:'))
        for count in range(num):
            name = input('name:')
            age = int(input('age:'))
            stu = {'name':name, 'age':age}
            self.students.append(stu)

    def del_student(self):
        print('删除学生')
        name = input('name')
        flag = True
        index_list = []
        for index in range(0, len(self.students)):
            if name == self.students[index]['name']:
                flag = False
                print(index, self.students[index])
                index_list.append(index)
        if flag:
            print('没有找到')
        else:
            del_index = int(input('请输入要删除学生的下标:'))
            if del_index in index_list:
                del self.students[del_index]
            else:
                print('下标输入错误')

    def look_student(self):
        print('查看学生')
        name = input('请输入要查看的学生名字:')
        flag = True
        for item in self.students:
            if name == item['name']:
                flag = False
                print(item)
        if flag:
            print('没有找到')

    def show_info(self):
        for item in self.students:
            print(item)

if __name__=='__main__':
    stu = {'name':'xxx', 'age':18}
    stu1 = []
    stu1.append(stu)
    cla1 = Class('py1805', stu1)

    cla1.add_student()
    cla1.del_student()
    cla1.look_student()
    print('=========')
    cla1.show_info()

>>>

添加学生
请输入添加学生人数:2
name:aa
age:1
name:bb
age:2
删除学生
namebb
2 {'name': 'bb', 'age': 2}
请输入要删除学生的下标:2
查看学生
请输入要查看的学生名字:aa
{'name': 'aa', 'age': 1}
=========
{'name': 'xxx', 'age': 18}
{'name': 'aa', 'age': 1}
上一篇下一篇

猜你喜欢

热点阅读