Chp 8
# 8-12
def make_sandwiches(*food):
print('sandwiches has', food)
make_sandwiches('egg')
make_sandwiches('egg', 'bread')
make_sandwiches('egg', 'bread', 'hot dog')
# 8-14
def make_car(producer, model, **kw):
ret = {}
ret['producer'] = producer
ret['model'] = model
for key in kw:
ret[key] = kw[key]
return ret
car = make_car('subaru', 'outback',
color='blue', tow_package=True)
chp 9
# 9-1
class Restaurant(object):
def __init__(self, rn, ct):
self.restaurant = rn
self.cuisine_type = ct
def describe_restaurant(self):
print(self.restaurant, self.cuisine_type)
def open_restaurant(self):
print('yes, it is')
restaurant = Restaurant('Tasty House', 'Chinese food')
restaurant.describe_restaurant()
restaurant.open_restaurant()
# 9-2
r1 = Restaurant('Good Restaurant', 'Chinese')
r2 = Restaurant('Beautiful Restaurant', 'Chinese')
r3 = Restaurant('Delicious Restaurant', 'Japaness')
r1.describe_restaurant()
r1.open_restaurant()
r2.describe_restaurant()
r2.open_restaurant()
r3.describe_restaurant()
r3.open_restaurant()
# 9-3
class User(object):
def __init__(self, firstname, lastname, age):
self.fn = firstname
self.ln = lastname
self.age = age
def __str__(self):
return "username: {} {}, age: {}".format(self.ln, self.fn, age)
def describe_user(self):
print(self)
def greet_msg(self):
return "hello {} {}, how are u?".format(self.ln, self.fn)
def greet(self):
print(self.greet_msg())
# 9-4
class Restaurant(object):
def __init(self, ...):
...
self.served_num = 0
def set_number_serverd(num):
self.served_num = num
print(self.served_num)
def increment_number_served():
self.served_num += 1
# 9-6
class IceCreamStand(Restaurant):
def __init__(self, rn, favours):
super().__init__(self, rn, "ice cream")
self.favours = favour
def show_ice_cream(self):
msg = "ice cream list are:\n"
for favour in favours:
msg += str(favour) + " favour\n"
print(msg)
iceCreamStand = IceCreamStand('tasty ic', ['chocolate', 'milk'])
class Privilege(User):
def __init__(self, firstname, lastname, age, privilege):
super().__init__(self, firstname, lastname, age)
self.privilege = privilege
def show_privileges(self):
self.describe_user(self)
for p in privilege:
print(p)
admin = Privilege('Ben', 'Yan', 30, [
'can add file',
'can delete file',
'can ban user'
])
admin.show_privileges()
# 9-7
class Privileges(object):
def __init__(self, privileges):
self.privileges = privileges
def show_privileges(self):
ret = ""
for p in self.privileges:
ret += p + '\n'
return ret
class Privilege(User):
def __init__(self, firstname, lastname, age, privilege):
super().__init__(self, firstname, lastname, age)
self.privileges = Privileges(privilege)
def show_privileges(self):
self.describe_user(self)
self.privileges.show_privileges()
# 9-10
from Restaurant import *
res = Restaurant('Good Restaurant', 'Chinese')
res.describe_restaurant()
# 9-14
from random import random
class Die(object):
def __init__(self, side = 6):
self.side = 6
def rolling(self):
return random.randint(1, self.side+1)
d10 = Die(10)
d20 = Die(20)
print('rolling die 10')
for i in range(10):
print('#{}: {}'.format(i, d10.rolling()))
print('rolling die 20')
for i in range(10):
print('#{}: {}'.format(i, d20.rolling()))