我的人工智能之路1-Python编程:从入门到实践-2


今天已经学到第9章了,类,有些练习答案分享如下:

9-6:冰淇淋小店

class Restaurant():
    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 0

    def open_restaurant(self):
        print("The restaurant is opening")

    def describe_restaurant(self):
        print("The restaurant name is " + self.restaurant_name +
              ", and the cuisine type is " + self.cuisine_type + ".")

    def set_number_served(self, num):
        self.number_served = num

    def increment_number_served(self,inc):
        self.number_served += inc


class IceCreamStand(Restaurant):
    def __init__(self, restaurant_name, cuisine_type, *flavors):
        super().__init__(restaurant_name, cuisine_type)
        self.flavors = flavors

    def back(self):
        for i in self.flavors:
            print(i)

ice = IceCreamStand('waipojia', 'zhongcan','a','b','c')
ice.back()

9-7:管理员

class User():

    def __init__(self,first_name,last_name):
        self.first_name = first_name
        self.last_name = last_name

    def describe_user(self):
        print(self.first_name.title() + ' ' +
              self.last_name.title() +
              " is a user!")

class Admin(User):

    def __init__(self,first_name,last_name,*list):
        super().__init__(first_name,last_name)
        self.privileges = list

    def show_privileges(self):
        print(self.first_name.title() + ' ' +
              self.last_name.title())
        for i in self.privileges:
            print('\t-' + i + '.')

user = Admin('john', 'haha', 'can add post', 'can delete post', 'can ban user')
user.show_privileges()
9-8:权限

class User():

    def __init__(self,first_name,last_name):
        self.first_name = first_name
        self.last_name = last_name

    def describe_user(self):
        print(self.first_name.title() + ' ' +
              self.last_name.title() +
              " is a user!")


class Privileges():
    def __init__(self):
        self.privileges = ['can add post', 'can delete post', 'can ban user']

    def show_privileges(self):
        for i in self.privileges:
            print('\t-' + i + '.')

class Admin(User):

    def __init__(self,first_name,last_name):
        super().__init__(first_name,last_name)
        self.privileges = Privileges()


user = Admin('john', 'haha')
user.privileges.show_privileges()

9-9:电瓶升级

class Car():
    """一次模拟汽车的简单尝试"""
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_descriptive_name(self):
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name.title()

    def read_odometer(self):
        print("This car has " + str(self.odometer_reading) + " miles on it.")

    def update_odometer(self, mileage):
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You can't roll back an odometer!")

    def increment_odometer(self, miles):
        self.odometer_reading += miles

class Battery():
    """一次模拟电动汽车电瓶的简单尝试"""

    def __init__(self, battery_size=70):
        """初始化电瓶的属性"""
        self.battery_size = battery_size

    def describe_battery(self):
        """打印一条描述电瓶容量的消息"""
        print("This car has a " + str(self.battery_size) + "-kWh battery.")

    def get_range(self):
        """打印一条消息,指出电瓶的续航里程"""
        if self.battery_size == 70:
            range = 240
        elif self.battery_size == 85:
            range = 270
        message = "This car can go approximately " + str(range)
        message += " miles on a full charge."
        print(message)

    def upgrade_battery(self):
        self.battery_size = 85


class ElectricCar(Car):
    """电动汽车的独特之处"""
    def __init__(self, make, model, year):
        """初始化父类的属性"""
        super().__init__(make, model, year)
        self.battery = Battery()


my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
my_tesla.battery.upgrade_battery()
my_tesla.battery.get_range()

9-14:骰子

#die.py
from random import randint

class Die():

    def __init__(self, sides=6):
        self.sides = sides

    def roll_die(self):
        print(randint(1, self.sides))
#my_die.py
import die

my_die = die.Die(20)
for i in range(10):
    my_die.roll_die()







你可能感兴趣的:(我的人工智能之路1-Python编程:从入门到实践-2)