#1.餐馆:创建一个名为Restaurant的类,其方法_init_()设置两个属性:restaurant_name和cuisine_type。 #创建一个名为describe_restaurant的方法和一个名为open_restaurant()的方法, #其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业 #根据这个类创建一个实例restaurant,分别打印其两个属性,再调用前述两个方法 class Restaurant(): #餐馆的基本信息 def __init__(self,restaurant_name,cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type def describe_restaurant(self): print("The restaurant name:" + self.restaurant_name.title()) print("The restaurant's cuisine type:" + self.cuisine_type.title()) def open_restaurant(self): print("The " + self.restaurant_name.title() + " is opening...") restaurant = Restaurant('yican','zonghe') restaurant.describe_restaurant() restaurant.open_restaurant() #2.用户:创建一个名为User的类,其中包含属性first_name和last_name,还有用户简介通常会存储的其他几个属性 #在类User中定义一个名为describe_user()的方法,它打印用户信息摘要;再定义一个名为great_user()的方法,它向 #用户发出个性化的问候 #创建多个用户实例,并对每个实例都调用上述两个方法 class User(): #用户的基本信息 def __init__(self,first_name,last_name,career,position): self.first_name = first_name self.last_name = last_name self.position = position self.career = career def describe_user(self): print("The user's first name is: " + self.first_name) print("The user's last name is: " + self.last_name) print("His career is: " + self.career) print("He lives in: " + self.position) def great_user(self): print("Hello! " + self.last_name.title() + ' ' + self.first_name.title()) user1 = User('ming','li','student','yantai') user1.describe_user() user1.great_user() user2 = User('qiong','xiao','teacher','shanghai') user2.describe_user() user2.great_user() #1.冰淇淋小店:冰淇淋小店是一种特殊的餐馆。编写一个名为IceCreamStand的类,让它继承 #练习9-4编写的类。添加一个名为flavors的属性,用于存储一个由各种口味的冰淇淋组成的列表 #编写一个显示这些冰淇淋的方法,创建一个IceCreamStand实例,并调用这个方法 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("The restaurant name:" + self.restaurant_name.title()) print("The restaurant's cuisine type:" + self.cuisine_type.title()) def set_number_served(self,number): self.number_served = number print('the restaurant has ' + str(self.number_served) +' in it.') def increment_number_served(self,add_number): for i in range(1,6): self.number_served += add_number print('Day' + str(i) + ':restauranr has ' + str(self.number_served) + ' in it') def open_restaurant(self): print("The " + self.restaurant_name.title() + " is opening...") class IceCreamStand(Restaurant): def __init__(self,restaurant_name,cuisine_type): super().__init__(restaurant_name,cuisine_type) self.flavors = [] def show_icecream(self): self.flavors.append('apple') self.flavors.append('banana') self.flavors.append('tea') print("The kinds of icecream have:",self.flavors) icecream_list = IceCreamStand('百度饮品','饮品') icecream_list.show_icecream() #2.管理员:管理员是一种特殊的用户。编写一个名为Admin的类,让它继承9-5编写的User类 #添加一个名为privileges的属性,用于存储一个由字符串(如"can add post","can delete post")等组成的列表。 #编写一个名为show_privileges()的方法,它显示管理员的权限。创建一个Admin实例。 #并调用这个方法 class User(): #用户的基本信息 def __init__(self,first_name,last_name): self.first_name = first_name self.last_name = last_name self.login_attempts = 0 def describe_user(self): print("The user is : " + self.last_name + ' ' + self.first_name) def great_user(self): print("Hello! " + self.last_name.title() + ' ' + self.first_name.title()) def increment_login_attempts(self): self.login_attempts += 1 def reset_login_attempts(self): self.login_attempts = 0 print("the login number has setd to:" + str(self.login_attempts)) class Admin(User): def __init__(self,first_name,last_name): super().__init__(first_name,last_name) self.privileges = [] def show_privileges(self): self.privileges.append("can add user") self.privileges.append("can ban user") self.privileges.append("can delete user") print("The Administrator's privileges: ",self.privileges) administrator = Admin('明','晓') administrator.show_privileges() #3.权限:编写一个名为Privileges的类,它只有一个属性——privileges,其中 #存储在2中的字符串列表。将方法show_privileges()移到这个类中。在Admin类中 #将一个Privilege实例用作其属性,创建一个Admin实例,并使用方法show_privileges()来显示其权限 class User(): #用户的基本信息 def __init__(self,first_name,last_name): self.first_name = first_name self.last_name = last_name self.login_attempts = 0 def describe_user(self): print("The user is : " + self.last_name + ' ' + self.first_name) def great_user(self): print("Hello! " + self.last_name.title() + ' ' + self.first_name.title()) def increment_login_attempts(self): self.login_attempts += 1 def reset_login_attempts(self): self.login_attempts = 0 print("the login number has setd to:" + str(self.login_attempts)) class Privilege(): def __init__(self): self.privileges = ["can add user","can ban user","can delete user"] def show_privileges(self): print(self.privileges) class Admin(User): def __init__(self,first_name,last_name): super().__init__(first_name,last_name) self.privileges = Privilege() def show_privileges(self): self.privileges.show_privileges() admin = Admin('ming','xiao') admin.show_privileges() #4.电瓶升级:最本节最后一个electric.py版本中,给Battery类添加一个名为upgrade_battery()方法。 #这个方法检查电瓶容量,如果它不是85,就将设为85.创建一辆电瓶容量默认值的电动汽车,调用方法get_range() #然后对电瓶进行升级,并再次调用get_range(),你会看到这辆汽车的续航里程增加 class Car(): """docstring for 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 #打印里程消息 def read_odometer(self): print("This car has " + str(self.odometer_reading) + " miles on it.") #更新里程数 def update_odometer(self,milegeage): if milegeage >= self.odometer_reading: self.odometer_reading = milegeage else: print("You can't roll back an odometer!") def increment_odometer(self,miles): self.odometer_reading += miles class Battery(): """docstring for 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 upgrade_battery(self): if self.battery_size!=85: self.battery_size = 85 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) self.upgrade_battery() class ElectricCar(Car): """docstring for ElectricCar""" def __init__(self, make,model,year): super().__init__(make,model,year) self.battery_size = Battery() def describe_battery(self): self.battery_size.battery_size() def get_range(self): self.battery_size.get_range() my_tesla = ElectricCar('tesla','model s',2016) my_tesla.get_range() my_tesla.get_range() #1.导入Restaurant类:将最新的Restaurant类存储在一个模块中。在另一个文件中, #导入Restaurant类,创建一个Restaurant的实例,并调用Restaurant的一个方法, #以确认import语句正确无误 #第一种方式 import restaurant test_restaurant = restaurant.Restaurant('K','comprehention') test_restaurant.describe_restaurant() #第二种方式 from restaurant import Restaurant test = Restaurant('K','mix') test.describe_restaurant() #2.导入Admin类:将9-8完成的工作为基础,将User、Privileges和Admin类存储在一个模块中 #再创建一个文件,在其中创建一个Admin实例并对其调用方法show_privileges(),以确认一切都能正常运行 from user1 import Admin admin = Admin('ming','xiao') admin.show_privileges() #3.多个模块:将User类存储在一个模块中,并将Privileges和Admin类存储在另一个模块中 #再创建一个文件夹,在其中创建一个Admin实例,并对其调用方法Show_privileges(),以确认一切能正常运行 from user3 import Admin admin = Admin('ming','xiao') admin.show_privileges() #4.使用OrderedDict: from collections import OrderedDict favorite_languages = OrderedDict() favorite_languages['jen'] = 'python' favorite_languages['sarah'] = 'C' favorite_languages['edward'] = 'ruby' favorite_languages['phil'] = 'java' for name,language in favorite_languages.items(): print(name.title() + "'s favorite language is " + language.title() + '.') #5.骰子:模块random包含以各种方式生成随机数的函数,其中的randint()返回一个位于指定范围内的整数 #创建一个Die类,它包含一个名为sides的属性,该属性的默认值为6.编写一个名为roll_die的方法,它打印位于 #1和骰子面数之间的随机数。创建一个6面的骰子,再掷10次 #再创建一个10面和20面的骰子,并将它们投掷10次 from random import randint class Die(): def __init__(self,sides6=6,sides10=10,sides20=20): self.sides6 = sides6 self.sides10 = sides10 self.sides20 = sides20 def roll_die6(self): self.random_number1 = randint(1,self.sides6) print(self.random_number1) def roll_die10(self): self.random_number2 = randint(1,self.sides10) print(self.random_number2) def roll_die20(self): self.random_number3 = randint(1,self.sides20) print(self.random_number3) die_num = Die() print("6面:") for i in range(1,11): die_num.roll_die6() print(' 10面:') for i in range(1,11): die_num.roll_die10() print(' 20面:') for i in range(1,11): die_num.roll_die20()