9-1,餐馆
class Restaurant():
def __init__(self, name, type):
self.restaurant_name = name
self.cuisine_type = type
def describe_resurant(self):
print('restaurant_name: ' + self.restaurant_name)
print('cuisine_type: ' + self.cuisine_type)
def open_restaurant(self):
print('The restaurant is open.')
restaurant = Restaurant('CLL', 'Chinese')
print(restaurant.restaurant_name)
print(restaurant.cuisine_type)
restaurant.describe_resurant()
restaurant.open_restaurant()
![](https://i-blog.csdnimg.cn/blog_migrate/f352d2453172647f01041037627a4451.png)
9-3,用户
class User():
def __init__(self, first_name, last_name, job):
self.first_name = first_name
self.last_name = last_name
self.job = job
def describe_user(self):
print('first_name: ' + self.first_name)
print('last_name: ' + self.last_name)
print('job: ' + self.job)
def greet_user(self):
print('Hello, ' + self.first_name + ' ' + self.last_name)
user1 = User('Li', 'Xiao', 'IT')
user2 = User('Wang', 'Xiao', 'student')
user1.describe_user()
user1.greet_user()
user2.describe_user()
user2.greet_user()
![](https://i-blog.csdnimg.cn/blog_migrate/75f9146de99eddfac77364b3681e0f62.png)
9-4,就餐人数
class Restaurant():
def __init__(self, name, type, number_served = 0):
self.restaurant_name = name
self.cuisine_type = type
self.number_served = number_served
def describe_resurant(self):
print('restaurant_name: ' + self.restaurant_name)
print('cuisine_type: ' + self.cuisine_type)
def people(self):
print('There are ' + str(self.number_served) + 'people had meat here.')
def open_restaurant(self):
print('The restaurant is open.')
def set_number_served(self, number):
self.number_served = number
restaurant = Restaurant('CLL', 'Chinese')
restaurant.people()
restaurant.set_number_served(10)
restaurant.people()
![](https://i-blog.csdnimg.cn/blog_migrate/86437f382daa010b9b9f3b640913df5e.png)
9-6,
class Restaurant():
def __init__(self, name, type, number_served = 0):
self.restaurant_name = name
self.cuisine_type = type
self.number_served = number_served
def describe_resurant(self):
print('restaurant_name: ' + self.restaurant_name)
print('cuisine_type: ' + self.cuisine_type)
def people(self):
print('There are ' + str(self.number_served) + 'people had meat here.')
def open_restaurant(self):
print('The restaurant is open.')
def set_number_served(self, number):
self.number_served = number
class IceCreamStand(Restaurant):
def __init__(self, name, type, number_served, flavors):
super().__init__(name, type, number_served)
self.flavors = flavors[:]
def printCream(self):
for iceCream in self.flavors:
print(iceCream)
IceCream = IceCreamStand('CCL', 'IceCream', 1000, ['Strawberry Ice Cream', 'Watermelon ice cream',
'Tomato ice cream', 'Lemon ice cream'])
IceCream.printCream()
![](https://i-blog.csdnimg.cn/blog_migrate/2fe96497ea34bf8ba0f8cf933acf469d.png)
from collections import OrderedDict
dictionary = OrderedDict()
dictionary['str'] = 'change the value into string'
dictionary['def'] = 'define a function'
dictionary['class'] = 'define a class'
dictionary['return'] = 'use "return" to return a value in a function'
dictionary['int'] = 'change value into the type-int'
for key, value in dictionary.items():
print(key + ':' + value)
![](https://i-blog.csdnimg.cn/blog_migrate/6eaa390a394b28c283dde5fd6e884e77.png)
9-14,骰子
from random import randint
class Die():
def __init__(self, side = 6):
self.side = side
def roll_die(self):
x = randint(1, self.side)
print(x, end=' ')
die1 = Die()
print("die1:")
for i in range(10):
die1.roll_die()
print()
die2 = Die(10)
print('die2:')
for i in range(10):
die2.roll_die()
print()
die3 = Die(20)
print('die3:')
for i in range(20):
die3.roll_die()
print()
![](https://i-blog.csdnimg.cn/blog_migrate/aa989a408c2cf4caa0694030973d57c3.png)