Python作业9-4就餐人数

参考书:《Python编程从入门到实践》
作业:9-4 就餐人数

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# Please write your outline & code.


class Restaurant:
    """餐馆的相关信息"""

    # 构造函数
    def __init__(self, restaurant_name, cuisine_type):
        # 上一句,其中 restaurant_name 是参数
        self.name = restaurant_name  # 餐馆的名字,其中 name 是属性
        self.type = cuisine_type  # 菜式
        self.number_served = 0

    # 初始化属性
    def describe_restaurant(self):
        content1 = f"The name of the restaurant is {self.name}.\nThe cuisine of the restaurant is {self.type}."
        return content1

    def open_restaurant(self):
        content2 = "The restaurant is open now."
        return content2

    def get_number_served(self, number):
        if number >= self.number_served:
            self.number_served += number
            number_served = f"There are {self.number_served} guests today."
        else:
            number_served = "It's not open today."
        return number_served

    def increment_number_served(self, increment_number):
        self.number_served += increment_number
        # pass
        increment_number2 = f"Today the number has increased to a total of {self.number_served}."
        return increment_number2


# 实例化调用
restaurant = Restaurant("张三的小店", "本地菜")
print(restaurant.describe_restaurant())
print(restaurant.open_restaurant())

print("-----------分隔符-------------")

restaurant = Restaurant("KFC", "鸡腿汉堡方便面")
print(restaurant.describe_restaurant())
print(restaurant.open_restaurant() + "\n")

# 调用其他属性
print(f"{restaurant.get_number_served(number=35)}")
print(f"{restaurant.increment_number_served(100)}")

调用函数,返回值出现 None 的解决办法:

  • 将函数 def 中的 print 改为 str=“xxx” 的表述形式,再用 return str 进行返回;
  • 重新调用,可发现 None 已消失。

你可能感兴趣的:(练习题,python,类)