python从入门到实践课后习题第九章

"""
9-1 餐馆 :创建一个名为 Restaurant 的类,其方法 __init__() 设置两个属性: restaurant_name 和 cuisine_type 。
创建一个名为 describe_restaurant() 的方法和一个名为 open_restaurant() 的方法,
其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。
根据这个类创建一个名为 restaurant 的实例,
分别打印其两个属性,再调用前述两个方法。
"""


class Restaurant:

    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print(self.restaurant_name + " is a " + self.cuisine_type + "cuisine_type")

    def open_restaurant(self):
        print(self.restaurant_name + " In the business")


restaurant = Restaurant('Hunan dishes', 'Hunan Cuisine')
restaurant.describe_restaurant()
restaurant.open_restaurant()
 
"""
9-2 三家餐馆 :根据你为完成练习 9-1 而编写的类创建三个实例,并对每个实例调用方法 describe_restaurant() 。
"""


class Restaurant:

    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print(self.restaurant_name + " is a " + self.cuisine_type + "cuisine_type")

    def open_restaurant(self):
        print(self.restaurant_name + " In the business")


restaurant = Restaurant('Hunan dishes', 'Hunan Cuisine')
restaurant.describe_restaurant()
restaurant.open_restaurant()

restaurant_2 = Restaurant('Gang Xuan', 'Hong Kong tea restaurant')
restaurant_2.describe_restaurant()

restaurant_3 = Restaurant('The corn meal', 'pasta')
restaurant_3.describe_restaurant()
"""
9-3 用户 :创建一个名为 User 的类,其中包含属性 first_name 和 last_name ,
还有用户简介通常会存储的其他几个属性。在类 User 中定义一个名为 describe_user() 的方法,
它打印用户信息摘要;再定义一个名为 greet_user() 的方法,它向用户发出个性化的问候。
"""


class User:

    def __init__(self, first_name, last_name, age, sex, tel):
        self.full_name = first_name + last_name
        self.age = str(age)
        self.tel = tel
        self.sex = sex

    def describe_user(self):

        if self.sex == 'male':
            print(self.full_name + " is " + self.age + " and his telephone number is " + self.tel)
        else:
            print(self.full_name + " is " + self.age + " and her telephone number is " + self.tel)

    def greet_user(self):
        print(self.full_name + "Welcome to back!~")


user = User('He', 'louie', 23, 'male', '13347335633')
user.describe_user()
user.greet_user()
"""
9-4 就餐人数 :在为完成练习 9-1 而编写的程序中,添加一个名为 number_served 的属性,并将其默认值设置为 0 。
根据这个类创建一个名为 restaurant 的实例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。
添加一个名为 set_number_served() 的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。
添加一个名为 increment_number_served() 的方法,它让你能够将就餐人数递增。
调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。
"""


class Restaurant:

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

    def describe_restaurant(self):
        print(self.restaurant_name + " is a " + self.cuisine_type + "cuisine_type")

    def open_restaurant(self):
        print(self.restaurant_name + " In the business")

    def set_number_served(self):
        self.number_served = 5
        print(str(self.number_served) + " people have dined here!")

    def increment_number_served(self):
        self.number_served = self.number_served + 1
        print(str(self.number_served) + " people have dined here!")


restaurant = Restaurant('Hunan dishes', 'Hunan Cuisine')
# restaurant.describe_restaurant()
# restaurant.open_restaurant()

restaurant.set_number_served()
restaurant.increment_number_served()
"""
9-5 尝试登录次数 :在为完成练习 9-3 而编写的 User 类中,添加一个名为 login_attempts 的属性。
编写一个名为 increment_login_attempts() 的方法,它将属性 login_attempts 的值加 1 。
再编写一个名为 reset_login_attempts() 的方法,它将属性 login_attempts 的值重置为 0 。
根据 User 类创建一个实例,再调用方法 increment_login_attempts() 多次。打印属性 login_attempts 的值,
确认它被正确地递增;然后,调用方法 reset_login_attempts() ,
并再次打印属性 login_attempts 的值,确认它被重置为 0 。
"""


class User:

    def __init__(self, first_name, last_name, age, sex, tel):
        self.full_name = first_name + last_name
        self.age = str(age)
        self.tel = tel
        self.sex = sex
        self.login_attempts = 0

    def describe_user(self):

        if self.sex == 'male':
            print(self.full_name + " is " + self.age + " and his telephone number is " + self.tel)
        else:
            print(self.full_name + " is " + self.age + " and her telephone number is " + self.tel)

    def greet_user(self):
        print(self.full_name + "Welcome to back!~")

    def increment_login_attempts(self):
        active = True
        while active:
            if self.login_attempts == 10:
                active = False
            else:
                self.login_attempts = self.login_attempts + 1
                print(self.login_attempts)

    def reset_login_attempts(self):
        self.login_attempts = 0
        print(self.login_attempts)


user = User('He', 'louie', 23, 'male', '13347335633')

user.increment_login_attempts()
user.reset_login_attempts()
 
"""
9-6 冰淇淋小店 :冰淇淋小店是一种特殊的餐馆。编写一个名为 IceCreamStand 的类,
让它继承你为完成练习 9-1 或练习 9-4 而编写的 Restaurant 类。这两个版本的 Restaurant 类都可以,
挑选你更喜欢的那个即可。添加一个名为 flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。
编写一个显示这些冰淇淋的方法。创建一个 IceCreamStand 实例,并调用这个方法。
"""


class Restaurant:

    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 0
        self.flavors = []

    def describe_restaurant(self):
        print(self.restaurant_name + " is a " + self.cuisine_type + "cuisine_type")

    def open_restaurant(self):
        print(self.restaurant_name + " In the business")

    def set_number_served(self):
        self.number_served = 5
        print(str(self.number_served) + " people have dined here!")

    def increment_number_served(self):
        self.number_served = self.number_served + 1
        print(str(self.number_served) + " people have dined here!")


class IceCreamStand(Restaurant):
    def show_icecream(self):
        print("There are varieties of ice cream:")
        for flavor in self.flavors:
            print(flavor)

    def recived_flavors(self):
        avtive = True

        while avtive:
            if self.flavors.__len__() < 5:
                flavor = input("Please enter your favorite ice cream:\n")
                self.flavors.append(flavor)
            else:
                avtive = False


icecreamstand = IceCreamStand('Hunan dishes', 'Hunan Cuisine')
icecreamstand.recived_flavors()
icecreamstand.show_icecream()

 
"""
9-7 管理员 :管理员是一种特殊的用户。编写一个名为 Admin 的类,让它继承你为完成练习 9-3 或练习 9-5 而编写的 User 类。
添加一个名为 privileges 的属性,用于存储一个由字符串(如 "can add post" 、 "can delete post" 、
"can ban user" 等)组成的列表。编写一个名为 show_privileges() 的方法,
它显示管理员的权限。创建一个 Admin 实例,并调用这个方法。
"""


class User:

    def __init__(self, first_name, last_name, age, sex, tel):
        self.full_name = first_name + last_name
        self.age = str(age)
        self.tel = tel
        self.sex = sex
        self.login_attempts = 0

    def describe_user(self):

        if self.sex == 'male':
            print(self.full_name + " is " + self.age + " and his telephone number is " + self.tel)
        else:
            print(self.full_name + " is " + self.age + " and her telephone number is " + self.tel)

    def greet_user(self):
        print(self.full_name + "Welcome to back!~")

    def increment_login_attempts(self):
        active = True
        while active:
            if self.login_attempts == 10:
                active = False
            else:
                self.login_attempts = self.login_attempts + 1
                print(self.login_attempts)

    def reset_login_attempts(self):
        self.login_attempts = 0
        print(self.login_attempts)


class Admin(User):
    def __init__(self, first_name, last_name, age, sex, tel):
        super().__init__(first_name, last_name, age, sex, tel)
        self.privileges = ['can add post', 'can delete post', 'can ban user']

    def show_privileges(self):
        for privilege in self.privileges:       # 将列表中privilege的值打印出来
            print(privilege)


user = Admin('He', 'louie', 23, 'male', '13347335633')

user.show_privileges()

 

"""
9-8 权限 :编写一个名为 Privileges 的类,它只有一个属性 —— privileges ,
其中存储了练习 9-7 所说的字符串列表。将方法 show_privileges() 移到这个类中。
在 Admin 类中,将一个 Privileges 实例用作其属性。创建一个 Admin 实例,
并使用方法 show_privileges() 来显示其权限。
"""


class User:

    def __init__(self, first_name, last_name, age, sex, tel):
        self.full_name = first_name + last_name
        self.age = str(age)
        self.tel = tel
        self.sex = sex
        self.login_attempts = 0

    def describe_user(self):

        if self.sex == 'male':
            print(self.full_name + " is " + self.age + " and his telephone number is " + self.tel)
        else:
            print(self.full_name + " is " + self.age + " and her telephone number is " + self.tel)

    def greet_user(self):
        print(self.full_name + "Welcome to back!~")

    def increment_login_attempts(self):
        active = True
        while active:
            if self.login_attempts == 10:
                active = False
            else:
                self.login_attempts = self.login_attempts + 1
                print(self.login_attempts)

    def reset_login_attempts(self):
        self.login_attempts = 0
        print(self.login_attempts)


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

    def show_privileges(self):
        for privilege in self.privileges:       # 将列表中privilege的值打印出来
            print(privilege)


class Admin(User):
    def __init__(self, first_name, last_name, age, sex, tel):
        super().__init__(first_name, last_name, age, sex, tel)
        self.privileges = Privileges()

    def show_privileges(self):
        self.privileges.show_privileges()


admin = Admin('He', 'louie', 23, 'male', '13347335633')

admin.show_privileges()

 

"""
9-9 电瓶升级 :在本节最后一个 electric_car.py 版本中,给 Battery 类添加一个名为 upgrade_battery() 的方法。
这个方法检查电瓶容量,如果它不是 85 ,就将它设置为 85 。创建一辆电瓶容量为默认值的电动汽车,
调用方法 get_range() ,然后对电瓶进行升级,并再次调用 get_range() 。
你会看到这辆汽车的续航里程增加了。
"""


class Car:
    """docstring for 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

    # 打印里程消息
    def read_odometer(self):
        print("This car has " + str(self.odometer_reading) + " miles on it.")

    # 更新里程数
    def update_odometer(self, milegeage):
        if milegeage >= self.odometer_reading:
            self.odometer_reading = milegeage
        else:
            print("You can't roll back an odometer!")

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


class Battery:
    """docstring for 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 upgrade_battery(self):
        if self.battery_size != 85:
            self.battery_size = 85

    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)
        self.upgrade_battery()


class ElectricCar(Car):
    """docstring for ElectricCar"""

    def __init__(self, make, model, year):
        super().__init__(make, model, year)
        self.battery_size = Battery()

    def describe_battery(self):
        self.battery_size.battery_size

    def get_range(self):
        self.battery_size.get_range()


my_tesla = ElectricCar('tesla', 'model s', 2016)
my_tesla.get_range()
my_tesla.get_range()
"""
9-10 导入 Restaurant 类 :将最新的 Restaurant 类存储在一个模块中。在另一个文件中,
导入 Restaurant 类,创建一个 Restaurant 实例,并调用 Restaurant 的一个方法,
以确认 import 语句正确无误。
"""

import restaurant

icecreamstand = restaurant.IceCreamStand('Hunan dishes', 'Hunan Cuisine')
icecreamstand.recived_flavors()
icecreamstand.show_icecream()
"""
9-11 导入 Admin 类 :以为完成练习 9-8 而做的工作为基础,将 User 、 Privileges 和 Admin
类存储在一个模块中,再创建一个文件,在其中创建一个 Admin 实例
并对其调用方法 show_privileges() ,以确认一切都能正确地运行。
"""

import admin

admin = admin.Admin('He', 'louie', 23, 'male', '13347335633')
admin.show_privileges()
"""
9-12 多个模块 :将 User 类存储在一个模块中,并将 Privileges 和 Admin 类存储在另一个模块中。
再创建一个文件,在其中创建一个 Admin 实例,并对其调用方法 show_privileges() ,
以确认一切都依然能够正确地运行。
"""


import admin

admin = admin.Admin('He', 'louie', 23, 'male', '13347335633')
admin.show_privileges()

 

转载于:https://www.cnblogs.com/xianyulouie/p/11041821.html

你可能感兴趣的:(python从入门到实践课后习题第九章)