《Python编程 从入门到实践》第九章习题选做

9-1 餐馆

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 + " main " + self.cuisine_type + ".")

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


re = Restaurant('Dasanyuan', 'Cantonese food')
print(re.restaurant_name + " " + re.cuisine_type)
re.describe_restaurant()
re.open_restaurant()

输出:

Dasanyuan Cantonese food
Dasanyuan main Cantonese food.
The restaurant is opening.

9-2 三家餐馆

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 + " main " + self.cuisine_type + ".")

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


re1 = Restaurant('Dasanyuan', 'Cantonese food')
re2 = Restaurant('White Swan', 'Western-style food')
re3 = Restaurant('Chongqing chicken soup', 'Hot Pot')
re1.describe_restaurant()
re2.describe_restaurant()
re3.describe_restaurant()

输出:

Dasanyuan main Cantonese food.
White Swan main Western-style food.
Chongqing chicken soup main Hot Pot.

9-3 用户

class User():
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name
        self.age = 23

    def describe_user(self):
        print("User name: " + self.first_name + " " + self.last_name)
        print("User age: " + str(self.age))

    def greet_user(self):
        print(
            "welcome to the party, " + self.first_name + " " + self.last_name)


user1 = User("Li", "Hua")
user2 = User("Steven", "Zhou")

user1.describe_user()
user1.greet_user()

user2.describe_user()
user2.greet_user()

输出:

User name: Li Hua
User age: 23
welcome to the party, Li Hua
User name: Steven Zhou
User age: 23
welcome to the party, Steven Zhou

9-4 就餐人数

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 + " main " + self.cuisine_type + ".")

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

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

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


restaurant = Restaurant('Dasanyuan', 'Cantonese food')
print(
    str(restaurant.number_served) + " people have eaten in " +
    restaurant.restaurant_name + ".")
restaurant.number_served = 12
print(
    str(restaurant.number_served) + " people have eaten in " +
    restaurant.restaurant_name + ".")
restaurant.set_number_served(34)
print(
    str(restaurant.number_served) + " people have eaten in " +
    restaurant.restaurant_name + ".")
restaurant.increment_number_served(10)
print(
    str(restaurant.number_served) + " people have eaten in " +
    restaurant.restaurant_name + ".")

输出:

0 people have eaten in Dasanyuan.
12 people have eaten in Dasanyuan.
34 people have eaten in Dasanyuan.
44 people have eaten in Dasanyuan.

9-5 尝试登录次数

class User():
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name
        self.age = 23
        self.login_attempts = 0

    def increment_login_attempts(self):
        self.login_attempts += 1

    def reset_login_attempts(self):
        self.login_attempts = 0

    def describe_user(self):
        print("User name: " + self.first_name + " " + self.last_name)
        print("User age: " + str(self.age))

    def greet_user(self):
        print(
            "welcome to the party, " + self.first_name + " " + self.last_name)

user1 = User("Li", "Hua")
print(user1.login_attempts)
for i in range(5):
    user1.increment_login_attempts()
    print(user1.login_attempts)
user1.reset_login_attempts()
print(user1.login_attempts)

输出:

0
1
2
3
4
5
0

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 describe_restaurant(self):
        print(self.restaurant_name + " main " + self.cuisine_type + ".")

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

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

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


class IceCreamStand(Restaurant):
    def __init__(self, restaurant_name, cuisine_type):
        super().__init__(restaurant_name, cuisine_type)
        self.flavors = [
            "ice cream", "frozen yogurt", "Fruit and vegetable ice cream",
            "Chocolate Ice Cream"
        ]

    def describe_flavors(self):
        for flavor in self.flavors:
            print(flavor)


ice = IceCreamStand("Ice cream shop", "Ice cream")
ice.describe_flavors()

输出:

ice cream
frozen yogurt
Fruit and vegetable ice cream
Chocolate Ice Cream

9-7 管理员

class User():
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name
        self.age = 23
        self.login_attempts = 0

    def increment_login_attempts(self):
        self.login_attempts += 1

    def reset_login_attempts(self):
        self.login_attempts = 0

    def describe_user(self):
        print("User name: " + self.first_name + " " + self.last_name)
        print("User age: " + str(self.age))

    def greet_user(self):
        print(
            "welcome to the party, " + self.first_name + " " + self.last_name)

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

    def show_privileges(self):
        for privilege in self.privileges:
            print(privilege)


user = Admin('Li', 'Hua')
user.show_privileges()

输出:

can add post
can delete post
can ban user

9-8 权限

class User():
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name
        self.age = 23
        self.login_attempts = 0

    def increment_login_attempts(self):
        self.login_attempts += 1

    def reset_login_attempts(self):
        self.login_attempts = 0

    def describe_user(self):
        print("User name: " + self.first_name + " " + self.last_name)
        print("User age: " + str(self.age))

    def greet_user(self):
        print(
            "welcome to the party, " + self.first_name + " " + self.last_name)


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:
            print(privilege)


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


user = Admin('Li', 'Hua')
user.privileges.show_privileges()

输出:

can add post
can delete post
can ban user
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值