9-1 Restaurants: create a file named Restaurant of the class with methods __init __ () to set two properties: restaurant_name and cuisine_type. Create a file called describe_restaurant () method and a method named open_restaurant (), in which the former print previous two messages, which prints a message stating that the restaurant is open.
According to the class to create a restaurant named examples, which are printed two properties, two of the calling method.
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.title()) print("Cuisine Type: " + self.cuisine_type.title()) def open_restaurant(self): """说明餐馆正在营业""" print("Welcome, our restaurant is open.") restaurant = Restaurant("big bowl noodle", "chinese")
print("Our restaurant'name is " + restaurant.restaurant_name.title() + ".") print("We often " + restaurant.cuisine_type.title() + " type foods.")
restaurant.describe_restaurant() restaurant.open_restaurant()
9-2 three restaurants: create three instances of the class based on the preparation of 9-1, and each instance of the call method describe_restaurant ().
restaurant1 = Restaurant("future future", "japanese") restaurant1.describe_restaurant() restaurant2 = Restaurant("Kyo-Chon", "korean type") restaurant2.describe_restaurant() restaurant3 = Restaurant("houcaller", "western type") restaurant3.describe_restaurant()
9-3 User: create a class called User, which contains attributes first_name and last_name, as well as user profile typically store several other properties. In the definition of a class called User describe the - user () method, which prints a summary of user information; then define a method named greet_user (), which generates personalized greeting to the user.
Create multiple instances represent different users, and each instance to call the above two methods.
class User: def __init__(self, first_name, last_name, age, address): """初始化用户的属性""" self.first_name = first_name self.last_name = last_name self.age = age self.address = address def describe_user(self): """打印用户信息摘要""" name = self.first_name.title() + " " + self.last_name.title() print('\n' + name + " " + self.age + " years old!") print("Live in " + self.address.title() + ".") def greet_user(self): """向用户问好""" name = self.first_name.title() + " " + self.last_name.title() print("Hello, " + name + "!") user1 = User('shirley'That',', '18', "xi'an") user1.describe_user() user1.greet_user() user2 = User('lucky', 'liu', '24', "beijin") user2.describe_user() user2.greet_user() user3 = User('suns', 'zhang', '20', "shanghai") user3.describe_user() user3.greet_user()
9-4 Number of dining: the procedure for the preparation of 9-1, add a property named number_served, and set its default value is 0. According to this class creates a named restaurant instance; print how many people have been eating at this restaurant, and then modify the value and print it again.
Add a method called set_number_served (), which allows you to set the number of diners. Call this method and pass it a value, and then print the value again.
Add a method called increment_served (), which allows you to increment the number of diners. Call this method passing it a such a value: Do you think the number of diners the restaurant likely to receive each day.
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.title()) print("Cuisine: " + self.cuisine_type.title()) DEF read_number_served (Self): "" " The number of meals in restaurants ever " "" Print ( " Our served " + str (self.number_served) + " . people \ the n- " ) DEF set_number_served (Self, Number The): "" " set the number of diners "" " self.number_served = number The DEF increment_number_served (Self, increment_number): " "" incremental number of diners "" " self.number_served = Self.number_served + increment_number restaurant = Restaurant("big bowl noodle", " Chinese " ) restaurant.describe_restaurant () restaurant.read_number_served () # print default on the number of people over meals restaurant.number_served = 200 # modified directly on the number of people who eat restaurant.read_number_served () # number of print after modification through meal restaurant .set_number_served ( 60) # call method to set the number of people dining restaurant.read_number_served () # number of the printing method call modification had meals restaurant.increment_number_served ( 140) # calls the method to increase the number of diners restaurant.read_number_served () # after printing increment the number of meals to over
9-5 attempts landing times: in the User class 9-3 written, add a property named login_attempts of. Write a method called increment_login_attempts (), the value of property login_attempts added. And then write a method named reset_login_attempts (), the value of the attribute login_attempts it is reset to 0.
According to create an instance of the User class, and then call the method increment_login_attempts () multiple times. Login_attempts print attribute values to confirm that it is properly incremented; then call the method reset_login_attempts (), and prints the value of the attribute login_attempts again confirm that it is reset to 0.
class User: def __init__(self, first_name, last_name): """初始化用户的属性""" self.first_name = first_name self.last_name = last_name self.login_attempts = 0 def greet_user(self): """向用户问好""" name = self.first_name.title() + " " + self.last_name.title() print("Hello, " + name + "!") defincrement_login_attempts (Self): "" " increase the number of log " "" self.login_attempts = self.login_attempts. 1 + DEF reset_login_attempts (Self): "" " Reset logins " "" self.login_attempts = 0 User = the User ( ' Mark ' , ' Sun ' ) user.greet_user () # call method increments logins for i in the Range (5 ): user.increment_login_attempts () Print ( " by You have have the Login " Str + (user.login_attempts) + " Times. " ) # Verify logins are correctly incremented user.reset_login_attempts () # call method to reset logins Print ( " by You the Login " + str (user.login_attempts) + " Time. " ) # to confirm logins are reset
9-6 ice cream shops: ice cream shop is a special restaurant. Write a function named IceCreamStand class, it inherits Restaurant category 9-1 or 9-4 written. Both versions of the Restaurant category can be, you can choose one. Add a property named flavors for storing a list of various flavors of ice cream composition. Writing a way to show these ice cream. IceCreamStand create an instance and call this method.
class IceCreamStand (Restaurant): DEF the __init__ (Self, RESTAURANT_NAME, cuisine_type): "" " all the properties of the parent class initialization " "" Super (). the __init__ (RESTAURANT_NAME, cuisine_type) self.flavors = [ ' Green TEA ' , ' RUM ' , ' Mango ' , ' Vanilla ' , ' Strawberry ' , ' Chocolate ' ] DEF show_icecream_flavors (Self): """ Show store all ice cream flavors""" print("We provide following flavors IceCream:") for flavor in self.flavors: print(flavor.title()) icecreamstand = IceCreamStand('love', 'icecream') icecreamstand.describe_restaurant() icecreamstand.show_icecream_flavors()