《Python编程-从入门到实践》第9、10章习题选练

本文档展示了Python编程的学习成果,包括创建Restaurant类及其子类IceCreamStand,涉及成员函数、属性和继承。同时介绍了文件操作,如读取文件、替换字符串以及处理用户输入的错误类型。
摘要由CSDN通过智能技术生成
9-1. Restaurant: Make a class called Restaurant. The __init__() method for Restaurant should store two attributes: a restaurant_name and a cuisine_type. Make a method called describe_restaurant() that prints these two pieces of information, and a method called open_restaurant() that prints a message indicating that the restaurant is open. Make an instance called restaurant from your class. Print the two attributes individually, and then call both methods.

9-2. Three Restaurants: Start with your class from Exercise 9-1. Create three different instances from the class, and call describe_restaurant() for each instance.

代码:

#9-1 & 9-2
class Restaurant():
    def __init__(self, restaurant_name, cuisine_name):
        self.restaurant_name = restaurant_name
        self.cuisine_name = cuisine_name
    
    def describe_restaurant(self):
        print("Restaurant name: " + self.restaurant_name.title())
        print("Cuisine name: " + self.cuisine_name.title())
        
    def open_restaurant(self):
        print(self.restaurant_name.title()+" is open")
        

kfc = Restaurant('kfc', 'chicken')
mc = Restaurant('MCDonald', 'beef')
king = Restaurant('burger king', 'ham')

kfc.describe_restaurant()
kfc.open_restaurant()

mc.describe_restaurant()
mc.open_restaurant()

king.describe_restaurant()
king.open_restaurant()

注意:“成员函数”参数勿忘加self & 访问“成员变量”时要加self


9-4. Number Served: Start with your program from Exercise 9-1.
Add an attribute called number_served with a default value of 0. Create an inst

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值