Python编程:从入门到实践(课后习题9)

来源:互联网 发布:冰魄寒鞭淘宝 编辑:程序博客网 时间:2024/05/17 21:45
# 9-1 餐馆class Restaurant():def __init__(self, restaurant_name, cuisine_type):self.restaurant_name = restaurant_nameself.cuisine_type = cuisine_typedef describe_restaurant(self):print("Restaurant name is " + self.restaurant_name + ".")print("Cuisine type is " + self.cuisine_type + ".")def open_restaurant(self):print("The restaurant is open.")restaurant = Restaurant("全聚德", "Peking duck")print(restaurant.restaurant_name, restaurant.cuisine_type)restaurant.describe_restaurant()  # 记得括号restaurant.open_restaurant()  # 记得括号# 9-2 三家餐馆dadong = Restaurant("北京大董", "Duck")yinxing = Restaurant("成都银杏南亭", "川、粤、西餐")weizhuang = Restaurant("味庄", "杭帮菜")dadong.describe_restaurant()yinxing.describe_restaurant()weizhuang.describe_restaurant()# 9-3 用户class User():def __init__(self, first_name, last_name, **kw):self.first_name = first_nameself.last_name = last_nameself.kw = kwdef decribe_user(self):print("first name: " + self.first_name.title() + ", last name: " + self.last_name.title() + ", others: " + str(self.kw))def greet_user(self):print("This is a personalized greeting, " + self.first_name.title())zhou = User('zhou', 'kai', girl_friend='Zhang Lili', birth_place='Chenzhou')zhang = User('zhang', 'lili', boy_friend='Zhou Kai', birth_place='Dezhou')zhou.decribe_user()zhou.greet_user()zhang.decribe_user()zhang.greet_user()# 9-4 就餐人数class Restaurant():def __init__(self, restaurant_name, cuisine_type):self.restaurant_name = restaurant_nameself.cuisine_type = cuisine_typeself.number_served = 0def describe_restaurant(self):print("Restaurant name is " + self.restaurant_name + ".")print("Cuisine type is " + self.cuisine_type + ".")print("This restaurant has a total of " + str(self.number_served) + " people to eat.")def open_restaurant(self):print("The restaurant is open.")# def set_number_served(self, number):  # 添加方法一# print("Number of meals: " + str(number) + ".")# def increment_number_served(self, add_number):  # 添加方法二# self.number_served += add_numberrestaurant = Restaurant("全聚德", "Peking duck")restaurant.describe_restaurant()restaurant.number_served = 10restaurant.describe_restaurant()# 9-5 尝试登陆次数class User():def __init__(self, first_name, last_name, **kw):self.first_name = first_nameself.last_name = last_nameself.login_attempts = 0self.kw = kwdef decribe_user(self):print("first name: " + self.first_name.title() + ", last name: " + self.last_name.title() + ", others: " + str(self.kw))def greet_user(self):print("This is a personalized greeting, " + self.first_name.title())def increment_login_attempts(self):self.login_attempts += 1def reset_login_attempts(self):self.login_attempts = 0admin = User('zhou', 'kai')admin.increment_login_attempts()admin.increment_login_attempts()admin.increment_login_attempts()admin.increment_login_attempts()print(admin.login_attempts)admin.reset_login_attempts()print(admin.login_attempts)# 9-6 冰淇淋小店# 父类class Restaurant():def __init__(self, restaurant_name, cuisine_type):self.restaurant_name = restaurant_nameself.cuisine_type = cuisine_typeself.number_served = 0def describe_restaurant(self):print("Restaurant name is " + self.restaurant_name + ".")print("Cuisine type is " + self.cuisine_type + ".")print("This restaurant has a total of " + str(self.number_served) + " people to eat.")def open_restaurant(self):print("The restaurant is open.")# 子类class IceCreamStand(Restaurant):def __init__(self, restaurant_name, cuisine_type, flavors):super().__init__(restaurant_name, cuisine_type)for i in flavors:print(i)# 9-7 管理员# 父类class User():def __init__(self, first_name, last_name):self.first_name = first_nameself.last_name = last_namedef decribe_user(self):print("first name: " + self.first_name.title() + ", last name: " + self.last_name.title())def greet_user(self):print("This is a personalized greeting, " + self.first_name.title())# 子类class Admin(User):def __init__(self, first_name, last_name):super().__init__(first_name, last_name)self.privileges = Privileges()list1 = ["can add post", "can delete post", "can ban user"]admin = Admin('zhou', 'kai', list1, age=20)admin.show_privileges()# 9-8 权限class Privileges():def __init__(self, privileges=["can add post", "can delete post", "can ban user"]):privi = []for x in privileges:privi.append(x)def show_privileges(self):print("You have all authority in User.")# 9-9 电瓶升级# 书中代码一class 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.title()    def read_odometer(self):        print("This car has " + str(self.odometer_reading) + " miles on it.")    def update_odometer(self, mileage):        if mileage >= self.odometer_reading:            self.odometer_reading = mileage        else:            print("You can't roll back an odometer!")    def increment_odometer(self, miles):        self.odometer_reading += miles# 书中代码二class Battery():        def __init__(self, battery_size=70):        self.battery_size = battery_size        def describe_bettery(self):        print("This ca has a " + str(self.battery_size) + "-kWh battery.")        def get_range(self):            if self.battery_size == 70:            range = 240        elif self.battery_size == 85:            range = 270    def upgrade_battery(self):    if self.battery_size != 85:    self.battery_size = 85                message = "This car can go approximately " + str(range)        message += " miles on a full charge."        print(message)# 书中代码三class ElectricCar(Car):        def __init__(self, make, model, year):        super().__init__(make, model, year)        self.battery_size = Battery()            def describe_battery(self):        print("This car has a " + str(self.battery_size) + "-kWh battery.")# 9-14 骰子from random import randintclass Die():def __init__(self):self.sides = 6def roll_die(self):y = []for i in range(10):y.append(randint(1, self.sides))print(y)die = Die()die.roll_die()die.sides = 10  # 10面die.roll_die()die.sides = 20  # 20面die.roll_die()


 
阅读全文
3 2
原创粉丝点击