第六章课后练习

1

class Student:
    ''' 描述学生的类'''
    def __init__(self, name, age, gender, phone, address, email):
        ''' 构造器 '''
        self.name = name
        self.age = age
        self.gender = gender
        self.phone = phone
        self.address = address
        self.email = email

    def eat(self, food):
        ''' 吃 '''
        print('%s正在吃%s' % (self.name, food))

    def drink(self, drink):
        ''' 喝 '''
        print('%s正在喝%s' % (self.name, drink))        
        
    def play(self, sport, other):
        ''' 玩 '''
        print('我今年%s岁,正和%s玩%s' % (self.age, other, sport)) 

    def sleep(self):
        ''' 睡 '''
        print('%s正在%s睡觉' % (self.name, self.address)) 

    def __repr__(self):
        return 'Student(name=%s, age=%d, phone=%s, address=%s, email=%s)' % (self.name, self.age, self.phone, self.address, self.email)
        
if __name__ == '__main__':
    stu = Student('孙悟空', 500, 'MALE', '02028309358',
        '灵台方寸山', '[email protected]')
    stu.eat('桃子')
    stu.drink('美酒')
    stu.play('棍子', '牛魔王')
    stu.sleep()

2

from student import Student

contacts = [Student('孙悟空', 500, 'MALE', '02028309358',
    '灵台方寸山', '[email protected]'),
     Student('牛魔王', 650, 'MALE', '02028309378',
    '积雷山摩云洞', '[email protected]'),
    Student('白骨精', 23, 'MALE', '13699881122',
    '白骨岭', '[email protected]'),
    Student('猪八戒', 500, 'MALE', '13588889999',
    '福临山山云栈洞', '[email protected]')]
def find_by_name(name):
    return [x for x in contacts if name in x.name]
def find_by_email(email):
    return [x for x in contacts if email in x.email]
def find_by_address(address):
    return [x for x in contacts if address in x.address]

if __name__ == '__main__':
    t = input('请输入查找的方式, 名字(n), Email(e), 地址(a): ')
    k = input('请输入查找的关键字: ')
    if t == 'n':
        print(find_by_name(k))
    elif t == 'e':
        print(find_by_email(k))
    elif t == 'a':
        print(find_by_address(k))
    else:
        print('输入有误!')   

3

class Point:
    ''' 描述点的类'''
    def __init__(self, x, y):
        ''' 构造器 '''
        self.x = x
        self.y = y

    def distance(self, other):
        ''' 计算两点之间距离 '''
        return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5

    def judge_triangle(self, p1, p2):
        ''' 判断3个点组成的三角形 '''
        self_p1 = self.distance(p1)
        self_p2 = self.distance(p2)
        p1_p2 = p1.distance(p2)
        # 如果self_p1的距离最大
        if self_p1 > self_p2 and self_p1 > p1_p2:
            if self_p1 > (self_p2 + p1_p2):
                print('不是三角形')
            else:
                print("钝角三角形") if self_p1 ** 2 > (self_p2 ** 2 + p1_p2 ** 2) \
                    else print("锐角三角形") if self_p1 ** 2 < (self_p2 ** 2 + p1_p2 ** 2) \
                    else print("直角三角形")
        # 如果self_p2的距离最大
        if self_p2 > self_p1 and self_p2 > p1_p2:
            if self_p2 > (self_p1 + p1_p2):
                print('不是三角形')
            else:
                print("钝角三角形") if self_p2 ** 2 > (self_p1 ** 2 + p1_p2 ** 2) \
                    else print("锐角三角形") if self_p2 ** 2 < (self_p1 ** 2 + p1_p2 ** 2) \
                    else print("直角三角形")
        # 如果p1_p2的距离最大
        if p1_p2 > self_p1 and p1_p2 > self_p2:
            if p1_p2 > (self_p1 + self_p2):
                print('不是三角形')
            else:
                print("钝角三角形") if p1_p2 ** 2 > (self_p1 ** 2 + self_p2 ** 2) \
                    else print("锐角三角形") if p1_p2 ** 2 < (self_p1 ** 2 + self_p2 ** 2) \
                    else print("直角三角形")
    def __repr__(self):
        return 'Point(x=%s, y=%s)' % (self.x, self.y)

if __name__ == '__main__':
    pt = Point(1, 1)
    print(pt.distance(Point(2, 3)))
    pt.judge_triangle(Point(4, 1), Point(5, 5))

4

import math

class Points:
    ''' 描述点的类 '''
    def __init__(self, x, y, z):
        ''' 构造器 '''
        self.x=x
        self.y=y
        self.z=z
    
    def __sub__(self, no):
        ''' 为减法提供支持的方法 '''
        return  Points((self.x-no.x),(self.y-no.y),(self.z-no.z))

    def dot(self, no):
        ''' 点积 '''
        return (self.x*no.x)+(self.y*no.y)+(self.z*no.z)

    def cross(self, no):
        ''' 叉积 '''
        return Points((self.y*no.z-self.z*no.y),(self.z*no.x-self.x*no.z),(self.x*no.y-self.y*no.x))
        
    def absolute(self):
        return pow((self.x ** 2 + self.y ** 2 + self.z ** 2), 0.5)
if __name__ == '__main__':
    points = list()
    print('请依次输入4个点的x y z(中间以空格隔开)')
    for i in range(4):
        a = list(map(float, input().split()))
        points.append(a)

    a, b, c, d = Points(*points[0]), Points(*points[1]), Points(*points[2]), Points(*points[3])
    X = (b - a).cross(c - b)
    Y = (c - b).cross(d - c)
    angle = math.acos(X.dot(Y) / (X.absolute() * Y.absolute()))

    print("%.2f" % math.degrees(angle))

5

class Transport:
    def move(self, distance):
        print('我移动了%s千米' % distance)
        
class Car(Transport):
    def __init__(self, name):
        self.name = name

    def move(self, distance):
        print('%s我在马路上开了%s千米' % (self.name, distance))

class Train(Transport):
    def __init__(self, speed):
        self.speed = speed
    def move(self, distance):
        print('我以速度%s在铁轨上走了%s千米' % (self.speed, distance))

class Plain(Transport):
    def fly(self, distance):
        print('我在天空飞了%s千米' % distance)
    
if __name__ == '__main__':
    c = Car('BMW')
    c.move(30.2)

    t = Train(300)
    t.move(230.5)

    p = Plain()
    p.move(3440.8)
    p.fly(3440.8)

你可能感兴趣的:(Python学习)