教材第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 =', self.restaurant_name)
print('cuisine_type =', self.cuisine_type)
def open_restaurant(self):
print('The restaurant is open.')
restaurant = Restaurant('KFC', 'fast food')
restaurant.describe_restaurant()
restaurant.open_restaurant()
9-2 三家餐馆
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 =', self.restaurant_name)
print('cuisine_type =', self.cuisine_type)
def open_restaurant(self):
print('The restaurant is open.')
restaurant1 = Restaurant('KFC', 'fast food')
restaurant2 = Restaurant('Mcdonald\'s', 'fast food')
restaurant3 = Restaurant('Starbucks', 'coffee')
restaurant1.describe_restaurant()
restaurant2.describe_restaurant()
restaurant3.describe_restaurant()
9-3 用户
class User():
def __init__(self, first_name, last_name, age, sex):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.sex = sex
def describe_user(self):
print('first name =', self.first_name)
print('last name =', self.last_name)
print('age =', self.age)
print('sex =', self.sex)
def greet_user(self):
print('Hello,', self.first_name.title(), self.last_name.title()+'!')
user1 = User('san', 'zhang', 12, 'male')
user1.describe_user()
user1.greet_user()
user2 = User('si', 'li', 14, 'female')
user2.describe_user()
user2.greet_user()
user3 = User('wu', 'wang', 12, 'female')
user3.describe_user()
user3.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 =', self.restaurant_name)
print('cuisine_type =', self.cuisine_type)
print('number_served =', self.number_served)
def open_restaurant(self):
print('The restaurant is open.')
def set_number_served(self, new_number):
self.number_served = new_number
def increment_number_served(self, adder):
self.number_served += adder
restaurant = Restaurant('KFC', 'fried chicken')
print('There are', restaurant.number_served, 'people have had dinner in this restaurant.')
restaurant.number_served = 12
print('There are', restaurant.number_served, 'people have had dinner in this restaurant.')
restaurant.set_number_served(0)
print('There are', restaurant.number_served, 'people have had dinner in this restaurant.')
restaurant.increment_number_served(10)
print('There are', restaurant.number_served, 'people have had dinner in this restaurant.')
9-5 尝试登录次数
class User():
def __init__(self, first_name, last_name, age, sex):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.sex = sex
self.login_attempts = 0
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
user = User('si', 'zhao', 19, 'female')
user.increment_login_attempts()
user.increment_login_attempts()
print(user.login_attempts)
user.reset_login_attempts()
print(user.login_attempts)
9-6 冰淇淋小店
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 =', self.restaurant_name)
print('cuisine_type =', self.cuisine_type)
def open_restaurant(self):
print('The restaurant is open.')
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type):
super().__init__(restaurant_name, cuisine_type)
self.flavors = ['milk', 'strawberry', 'melon', 'apple']
def show_flavors(self):
print(self.flavors)
ice_cream_stand = IceCreamStand('minzhi', 'ice cream')
ice_cream_stand.show_flavors()
9-7 管理员
class User():
def __init__(self, first_name, last_name, age, sex):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.sex = sex
def describe_user(self):
print('first name =', self.first_name)
print('last name =', self.last_name)
print('age =', self.age)
print('sex =', self.sex)
def greet_user(self):
print('Hello,', self.first_name.title(), self.last_name.title()+'!')
class Admin(User):
def __init__(self, first_name, last_name, age, sex):
super().__init__(first_name, last_name, age, sex)
self.privileges = ['can add post', 'can delete post', 'can ban user']
def show_privileges(self):
print(self.first_name.title(), self.last_name.title(), 'has some privileges:')
for privilege in self.privileges:
print(privilege)
admin = Admin('san', 'zhang', 25, 'male')
admin.show_privileges()
9-8 权限
class Privileges():
def __init__(self):
self.privileges = ['can add post', 'can delete post', 'can ban user']
def show_privileges(self):
for privilege in self.privileges:
print(privilege)
class Admin():
def __init__(self):
self.privileges = Privileges()
admin = Admin()
admin.privileges.show_privileges()
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_battery(self):
'''打印一条描述电瓶容量的消息'''
print('This car 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
message = "This car can go approximately " + str(range)
message += " miles on a full charge."
print(message)
def upgrade_battery(self):
if self.battery_size != 85:
self.battery_size = 85
class ElectricCar(Car):
'''Represent aspects of a car, specific to electric vehicles.'''
def __init__(self, make, model, year):
'''
电动汽车的独特之处
初始化父类的属性,再初始化电动汽车特有的属性
'''
super().__init__(make, model, year)
self.battery = Battery()
my_tesla = ElectricCar('tesla', 'model s', 2016)
my_tesla.battery.get_range()
my_tesla.battery.upgrade_battery()
my_tesla.battery.get_range()
9-10 导入Restaurant类
Restaurant.py
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 =', self.restaurant_name)
print('cuisine_type =', self.cuisine_type)
print('number_served =', self.number_served)
def open_restaurant(self):
print('The restaurant is open.')
def set_number_served(self, new_number):
self.number_served = new_number
def increment_number_served(self, adder):
self.number_served += adder
main.py
from Restaurant import Restaurant
restaurant = Restaurant('KFC', 'fried chicken')
restaurant.describe_restaurant()
9-11 导入Admin类
Users.py
class Privileges():
def __init__(self):
self.privileges = ['can add post', 'can delete post', 'can ban user']
def show_privileges(self):
for privilege in self.privileges:
print(privilege)
class User():
def __init__(self, first_name, last_name, age, sex):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.sex = sex
def describe_user(self):
print('first name =', self.first_name)
print('last name =', self.last_name)
print('age =', self.age)
print('sex =', self.sex)
def greet_user(self):
print('Hello,', self.first_name.title(), self.last_name.title()+'!')
class Admin(User):
def __init__(self, first_name, last_name, age, sex):
super().__init__(first_name, last_name, age, sex)
self.privileges = Privileges()
main.py
from Users import User, Privileges, Admin
admin = Admin('san', 'zhang', 18, 'male')
admin.privileges.show_privileges()
9-12 多个模块
Users.py
class User():
def __init__(self, first_name, last_name, age, sex):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.sex = sex
def describe_user(self):
print('first name =', self.first_name)
print('last name =', self.last_name)
print('age =', self.age)
print('sex =', self.sex)
def greet_user(self):
print('Hello,', self.first_name.title(), self.last_name.title()+'!')
Admin.py
from Users import User
class Privileges():
def __init__(self):
self.privileges = ['can add post', 'can delete post', 'can ban user']
def show_privileges(self):
for privilege in self.privileges:
print(privilege)
class Admin(User):
def __init__(self, first_name, last_name, age, sex):
super().__init__(first_name, last_name, age, sex)
self.privileges = Privileges()
main.py
from Admin import Admin
admin = Admin('san', 'zhang', 18, 'male')
admin.privileges.show_privileges()
9-13 使用OrderedDict
from collections import OrderedDict
words = OrderedDict()
words['C'] = 'A programming language.'
words['Python'] = 'A programming language.'
words['Java'] = 'A programming language.'
words['Google'] = 'An IT company.'
words['CPU'] = 'Central Processing Unit.'
words['Linux'] = 'An operating system.'
words['Windows'] = 'An operating system.'
words['DOS'] = 'An operating system.'
words['UNIX'] = 'An operating system.'
words['Turing'] = 'A computer scientist.'
for key, value in words.items():
print(key+':', value)
9-14 骰子
from random import randint
class Die():
def __init__(self, sides=6):
self.sides = sides
def roll_die(self):
print(randint(1, self.sides))
die = Die(6)
for i in range(10):
die.roll_die()
print()
die = Die(10)
for i in range(10):
die.roll_die()
print()
die = Die(20)
for i in range(10):
die.roll_die()
print()
9-15 Python Module of the Week
要了解Python标准库,一个很不错的资源是网站Python Module of the Week(https://pymotw.com/3/)。
下图是网站上collections模块的介绍。