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

# 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("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_name
		self.last_name = last_name
		self.kw = kw

	def 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_name
		self.cuisine_type = cuisine_type
		self.number_served = 0

	def 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_number

restaurant = Restaurant("全聚德", "Peking duck")
restaurant.describe_restaurant()

restaurant.number_served = 10
restaurant.describe_restaurant()

# 9-5 尝试登陆次数
class User():

	def __init__(self, first_name, last_name, **kw):
		self.first_name = first_name
		self.last_name = last_name
		self.login_attempts = 0
		self.kw = kw

	def 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 += 1

	def reset_login_attempts(self):
		self.login_attempts = 0

admin = 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_name
		self.cuisine_type = cuisine_type
		self.number_served = 0

	def 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_name
		self.last_name = last_name

	def 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 randint
class Die():

	def __init__(self):
		self.sides = 6

	def 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()
    原文作者:周小馬
    原文地址: https://blog.csdn.net/z120379372/article/details/78027732
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞