Python编程从入门到实践,第9章练习:9-1~9-15

练习 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(f"The name of the restaurant is {self.restaurant_name}.")
        print(f"The main cuisine in this restaurant is {self.cuisine_type}.")

    def open_restaurant(self):
        """描述餐馆的状态。"""
        print(f"{self.restaurant_name} is open")


restaurant = Restaurant('Delicious restaurant', 'Chinese food')

restaurant.describe_restaurant()
restaurant.open_restaurant()

输出结果:

The name of the restaurant is Delicious restaurant.
The main cuisine in this restaurant is Chinese food.
Delicious restaurant is open

练习 9-2:三家餐馆

根据为完成练习而编写的类创建三个实例,并对每个实例调用方法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(f"The name of the restaurant is {self.restaurant_name}.")
        print(f"The main cuisine in this restaurant is {self.cuisine_type}.")

    def open_restaurant(self):
        """描述餐馆的状态。"""
        print(f"{self.restaurant_name} is open.")

restaurant1 = Restaurant('Delicious restaurant', 'Chinese food')
restaurant2 = Restaurant('Philip', 'French cooking')
restaurant3 = Restaurant('Dessert master', 'Italian dessert')

restaurant1.describe_restaurant()
restaurant2.describe_restaurant()
restaurant3.describe_restaurant()

输出结果:

The name of the restaurant is Delicious restaurant.
The main cuisine in this restaurant is Chinese food.   
The name of the restaurant is Philip.
The main cuisine in this restaurant is French cooking. 
The name of the restaurant is Dessert master.
The main cuisine in this restaurant is Italian dessert.

练习 9-3:用户

创建一个名为User的类,其中包含属性first_name和last_name,以及用户简介通常会储存的其他几个属性。在类User中定义一个名为describe_user()的方法,用于向用户发出个性化的问候。

创建多个表示不同用户的实例,并对每个实例调用上述两个方法。

class User:
    """简单描述用户。"""

    def __init__(self, first_name, last_name, age, location):
        """初始化描述用户的属性。"""
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.location = location
    
    def describe_user(self):
        """简单描述用户。"""
        print("\nThe following is the user's information: ")
        print(f"\tName: {self.first_name} {self.last_name}")
        print(f"\tAge: {self.age}")
        print(f"\tLocation: {self.location}")
    
    def greet_user(self):
        """打印问候信息。"""
        full_name = f"{self.first_name} {self.last_name}"
        print(f"Hi,{full_name.title()},welcome on board.")



mark = User('Mark', 'Curli', 19, 'California')
lucy = User('Lucy', 'Lee', 16, 'London')
tiantian = User('TianTian', 'Xu', 21, 'HongKong')

mark.describe_user()
mark.greet_user()

lucy.describe_user()
lucy.greet_user()

tiantian.describe_user()
tiantian.greet_user()

输出结果:

The following is the user's information:
        Name: Mark Curli
        Age: 19
        Location: California
Hi,Mark Curli,welcome on board.

The following is the user's information:
        Name: Lucy Lee
        Age: 16
        Location: London
Hi,Lucy Lee,welcome on board.

The following is the user's information:
        Name: TianTian Xu
        Age: 21
        Location: HongKong
Hi,Tiantian Xu,welcome on board.

练习 9-4:就餐人数

贼为完成练习9-1而编写的程序中,添加一个名为number_served的属性,并将其默认值设置为0。根据这个类创建一个名为restaurant的实例。打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。

添加一个名为set_number_served()的方法,让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印它。

添加一个名为increment_number_served()的方法,让你能够将就餐人数递增。调用这个方法并向他传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。


                
  • 8
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值