python从入门到实践第九章的练习题作业

'''
9-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('restaurant_name is '+self.restaurant_name+'\n'+'cuisine_type is '+self.cuisine_type)

	def open_restaurant(self):
		print('Restaurant does business')

restaurant = Restaurant('ChinaseMan','Chinese Food')
print(restaurant.restaurant_name)
print(restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()

'''
9-2 三家餐馆:根据你为完成练习 9-1 而编写的类创建三个实例,并对每个实例调
用方法 describe_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('restaurant_name is '+self.restaurant_name+'\n'+'cuisine_type is '+self.cuisine_type)

	def open_restaurant(self):
		print('Restaurant does business')

r1 = Restaurant('ChinaseMan','Chinese Food')
r2 = Restaurant('AmericanMan','American Food')
r3 = Restaurant('RussianMan','Russian Food')
r1.describe_restaurant()
r2.describe_restaurant()
r3.describe_restaurant()

'''
9-3 用户:创建一个名为 User 的类,其中包含属性 first_name 和 last_name,还有
用户简介通常会存储的其他几个属性。在类 User 中定义一个名为 describe_user()的方
法,它打印用户信息摘要;再定义一个名为 greet_user()的方法,它向用户发出个性化
的问候。
创建多个表示不同用户的实例,并对每个实例都调用上述两个方法。
'''
class User():
	def __init__(self,first_name,last_name,idNumber,location = 'local'):
		self.first_name = first_name
		self.last_name = last_name
		self.id = idNumber
		self.location = location

	def describe_user(self):
		print('first_name : '+self.first_name+', last_name : '+self.last_name+ ' \nid : '+self.id+' \nlocation : '+self.location)

	def greet_user(self):
		print('hello, how are you, '+self.first_name+'.'+self.last_name+'?')
		print('your id is '+self.id+' , please remenber it.')
		print(self.location+' is a nice place!')

cc = User('cc','Char','10','U.S.A')
gg = User('gg','Lee','0','China')
jk = User('JK','Tako','1','Japan')

myList = [cc,gg,jk]
for value in myList:
	value.describe_user()
	value.greet_user()

'''
9-4 就餐人数:在为完成练习 9-1 而编写的程序中,添加一个名为 number_served
的属性,并将其默认值设置为 0。根据这个类创建一个名为 restaurant 的实例;打印有
多少人在这家餐馆就餐过,然后修改这个值并再次打印它。
添加一个名为 set_number_served()的方法,它让你能够设置就餐人数。调用这个
方法并向它传递一个值,然后再次打印这个值。
添加一个名为 increment_number_served()的方法,它让你能够将就餐人数递增。
调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。
'''
class Restaurant():
	'''
	#这种当然也可以
	def __init__(self,restaurant_name,cuisine_type):
		self.restaurant_name = restaurant_name
		self.cuisine_type = cuisine_type
		self.number_served = 0
	'''
	def __init__(self,restaurant_name,cuisine_type,number_served =0):
		self.restaurant_name = restaurant_name
		self.cuisine_type = cuisine_type
		self.number_served = number_served

	def describe_restaurant(self):
		print('restaurant_name is '+self.restaurant_name+'\n'+'cuisine_type is '+self.cuisine_type)

	def open_restaurant(self):
		print('Restaurant does business')

	def set_number_served(self ,num):
		self.number_served = num

	def increment_number_served(self,num):
		while self.number_served

'''
9-5 尝试登录次数:在为完成练习 9-3 而编写的 User 类中,添加一个名为
login_attempts 的属性。编写一个名为 increment_login_attempts()的方法,它将属性
login_attempts 的值加 1。再编写一个名为 reset_login_attempts()的方法,它将属性
login_attempts 的值重置为 0。
根据 User 类创建一个实例,再调用方法 increment_login_attempts()多次。打印属
性 login_attempts 的值,确认它被正确地递增;然后,调用方法 reset_login_attempts(),
并再次打印属性 login_attempts 的值,确认它被重置为 0。
'''
class User():
	def __init__(self,first_name,last_name,idNumber,login_attempts = 0,location = 'local'):
		self.first_name = first_name
		self.last_name = last_name
		self.id = idNumber
		self.location = location
		self.login_attempts = login_attempts

	def describe_user(self):
		print('first_name : '+self.first_name+', last_name : '+self.last_name+ ' \nid : '+self.id+' \nlocation : '+self.location)
		print('login_attempts is '+str(self.login_attempts))

		

	def greet_user(self):
		print('hello, how are you, '+self.first_name+'.'+self.last_name+'?')
		print('your id is '+self.id+' , please remenber it.')
		print(self.location+' is a nice place!')
		print('login_attempts is '+str(self.login_attempts))

	def increment_login_attempts(self):
		self.login_attempts += 1

	def reset_login_attempts(self):
		self.login_attempts = 0

u1 = User('cc','Char','10')
for value in range(0,7):
	u1.increment_login_attempts()
	u1.describe_user()

u1.reset_login_attempts()
u1.describe_user()

'''
9-6 冰淇淋小店:冰淇淋小店是一种特殊的餐馆。编写一个名为 IceCreamStand 的
类,让它继承你为完成练习 9-1 或练习 9-4 而编写的 Restaurant 类。这两个版本的
Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为 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 __init__(self,restaurant_name,cuisine_type,number_served =0):
		self.restaurant_name = restaurant_name
		self.cuisine_type = cuisine_type
		self.number_served = number_served

	def describe_restaurant(self):
		print('restaurant_name is '+self.restaurant_name+'\n'+'cuisine_type is '+self.cuisine_type)

	def open_restaurant(self):
		print('Restaurant does business')

	def set_number_served(self ,num):
		self.number_served = num

	def increment_number_served(self,num):
		while self.number_served

'''
9-7 管理员:管理员是一种特殊的用户。编写一个名为 Admin 的类,让它继承你为
完成练习 9-3 或练习 9-5 而编写的 User 类。添加一个名为 privileges 的属性,用于存
储一个由字符串(如"can add post"、 "can delete post"、 "can ban user"等)组成的
列表。编写一个名为 show_privileges()的方法,它显示管理员的权限。创建一个 Admin
实例,并调用这个方法。
'''
class User():
	def __init__(self,first_name,last_name,idNumber,login_attempts = 0,location = 'local'):
		self.first_name = first_name
		self.last_name = last_name
		self.id = idNumber
		self.location = location
		self.login_attempts = login_attempts

	def describe_user(self):
		print('first_name : '+self.first_name+', last_name : '+self.last_name+ ' \nid : '+self.id+' \nlocation : '+self.location)
		print('login_attempts is '+str(self.login_attempts))

		

	def greet_user(self):
		print('hello, how are you, '+self.first_name+'.'+self.last_name+'?')
		print('your id is '+self.id+' , please remenber it.')
		print(self.location+' is a nice place!')
		print('login_attempts is '+str(self.login_attempts))

	def increment_login_attempts(self):
		self.login_attempts += 1

	def reset_login_attempts(self):
		self.login_attempts = 0


class Admin(User):
	"""docstring for Admin"""
	def __init__(self, first_name,last_name,idNumber,login_attempts = 0,location = 'local',privileges = []):
		super().__init__(first_name,last_name,idNumber,login_attempts = 0,location = 'local')
		self.privileges = privileges
	def show_privileges(self):
		print('The Admin\'s privileges is '+str(self.privileges))
admin = Admin('cc','Char',10,1,'China',["can add post", "can delete post","can ban user"])
admin.show_privileges()

'''
9-8 权限:编写一个名为 Privileges 的类,它只有一个属性——privileges,其中
存储了练习 9-7 所说的字符串列表。将方法 show_privileges()移到这个类中。在 Admin
类中,将一个 Privileges 实例用作其属性。创建一个 Admin 实例,并使用方法
show_privileges()来显示其权限。
'''
class User():
	def __init__(self,first_name,last_name,idNumber,login_attempts = 0,location = 'local'):
		self.first_name = first_name
		self.last_name = last_name
		self.id = idNumber
		self.location = location
		self.login_attempts = login_attempts

	def describe_user(self):
		print('first_name : '+self.first_name+', last_name : '+self.last_name+ ' \nid : '+self.id+' \nlocation : '+self.location)
		print('login_attempts is '+str(self.login_attempts))

		

	def greet_user(self):
		print('hello, how are you, '+self.first_name+'.'+self.last_name+'?')
		print('your id is '+self.id+' , please remenber it.')
		print(self.location+' is a nice place!')
		print('login_attempts is '+str(self.login_attempts))

	def increment_login_attempts(self):
		self.login_attempts += 1

	def reset_login_attempts(self):
		self.login_attempts = 0

class Privileges():
	"""docstring for Privileges"""
	def __init__(self, privileges=[]):
		self.privileges = privileges
	def show_privileges(self):
		print('The Admin\'s privileges is '+str(self.privileges))


class Admin(User):
	"""docstring for Admin"""
	def __init__(self, first_name,last_name,idNumber,login_attempts = 0,location = 'local',privileges = []):
		super().__init__(first_name,last_name,idNumber,login_attempts = 0,location = 'local')
		self.privileges = Privileges(privileges)
	def show_privileges(self):
		self.privileges.show_privileges()
admin = Admin('cc','Char',10,1,'China',["can add post", "can delete post","can ban user"])
admin.show_privileges()


'''
9-10 导入 Restaurant 类:将最新的 Restaurant 类存储在一个模块中。在另一个文
件中,导入 Restaurant 类,创建一个 Restaurant 实例,并调用 Restaurant 的一个方法,
以确认 import 语句正确无误。
'''
from new_Restaurant import Restaurant

r1 = Restaurant('ChinaseMan','Chinese Food')
print('The number of people who have meals in this restaurant is '+str(r1.number_served))

'''
9-11 导入 Admin 类:以为完成练习 9-8 而做的工作为基础,将 User、 Privileges 和
Admin 类存储在一个模块中,再创建一个文件,在其中创建一个 Admin 实例并对其调用
方法 show_privileges(),以确认一切都能正确地运行。
'''
from Admin import Admin

admin = Admin('cc','Char',10,1,'China',["can add post", "can delete post","can ban user"])
admin.show_privileges()

'''
9-12 多个模块:将 User 类存储在一个模块中,并将 Privileges 和 Admin 类存储在
另一个模块中。再创建一个文件,在其中创建一个 Admin 实例,并对其调用方法
show_privileges(),以确认一切都依然能够正确地运行
'''
from  AdminAndPrivilege import Admin

admin = Admin('cc','Char',10,1,'China',["can add post", "can delete post","can ban user"])
admin.show_privileges()

'''
9-13 使用 OrderedDict: 在练习 6-4 中,你使用了一个标准字典来表示词汇表。请
使用 OrderedDict 类来重写这个程序,并确认输出的顺序与你在字典中添加键—值对的
顺序一致。
'''
class OrderedDict():
	"""docstring for OrderedDict"""
	def __init__(self,vocabulary):
		self.vocabulary =vocabulary
	def show_vocabulary(self):
		for key,value in self.vocabulary.items():
			print(key+ ' : ' + value)

vocabulary = {'int':'integer number','char':'the base element of string','class':'a base form of OOP'
				,'struct':'you can use this to develop your own data struct','printf':'a function to print something on the screen'}
o1 = OrderedDict(vocabulary)
o1.show_vocabulary()

'''
9-14 骰子:模块 random 包含以各种方式生成随机数的函数,其中的 randint()返回
一个位于指定范围内的整数,例如,下面的代码返回一个 1~6 内的整数:
from random import randint
x = randint(1, 6)
请创建一个 Die 类,它包含一个名为 sides 的属性,该属性的默认值为 6。编写一
个名为 roll_die()的方法,它打印位于 1 和骰子面数之间的随机数。创建一个 6 面的骰
子,再掷 10 次。
创建一个 10 面的骰子和一个 20 面的骰子,并将它们都掷 10 次。
'''
from random import randint

class Die():
	"""docstring for """
	def __init__(self, size = 6):
		self.size = size

	def roll_die(self):
		print(str(randint(1,self.size)))


d = Die()
print('A 10 tests.')
for x in range(1,11):
	d.roll_die()

print('A 20 tests')
for x in range(1,21):
	d.roll_die()

'''
Admin.py
'''

class User():
	def __init__(self,first_name,last_name,idNumber,login_attempts = 0,location = 'local'):
		self.first_name = first_name
		self.last_name = last_name
		self.id = idNumber
		self.location = location
		self.login_attempts = login_attempts

	def describe_user(self):
		print('first_name : '+self.first_name+', last_name : '+self.last_name+ ' \nid : '+self.id+' \nlocation : '+self.location)
		print('login_attempts is '+str(self.login_attempts))

		

	def greet_user(self):
		print('hello, how are you, '+self.first_name+'.'+self.last_name+'?')
		print('your id is '+self.id+' , please remenber it.')
		print(self.location+' is a nice place!')
		print('login_attempts is '+str(self.login_attempts))

	def increment_login_attempts(self):
		self.login_attempts += 1

	def reset_login_attempts(self):
		self.login_attempts = 0


class Admin(User):
	"""docstring for Admin"""
	def __init__(self, first_name,last_name,idNumber,login_attempts = 0,location = 'local',privileges = []):
		super().__init__(first_name,last_name,idNumber,login_attempts = 0,location = 'local')
		self.privileges = privileges
	def show_privileges(self):
		print('The Admin\'s privileges is '+str(self.privileges))

'''
AdminAndPrivilege.py
'''

from User import User

class Privileges():
	"""docstring for Privileges"""
	def __init__(self, privileges=[]):
		self.privileges = privileges
	def show_privileges(self):
		print('The Admin\'s privileges is '+str(self.privileges))


class Admin(User):
	"""docstring for Admin"""
	def __init__(self, first_name,last_name,idNumber,login_attempts = 0,location = 'local',privileges = []):
		super().__init__(first_name,last_name,idNumber,login_attempts = 0,location = 'local')
		self.privileges = Privileges(privileges)
	def show_privileges(self):
		self.privileges.show_privileges()

'''
IceCreamStand.py
'''


'''
9-6 冰淇淋小店:冰淇淋小店是一种特殊的餐馆。编写一个名为 IceCreamStand 的
类,让它继承你为完成练习 9-1 或练习 9-4 而编写的 Restaurant 类。这两个版本的
Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为 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 __init__(self,restaurant_name,cuisine_type,number_served =0):
		self.restaurant_name = restaurant_name
		self.cuisine_type = cuisine_type
		self.number_served = number_served

	def describe_restaurant(self):
		print('restaurant_name is '+self.restaurant_name+'\n'+'cuisine_type is '+self.cuisine_type)

	def open_restaurant(self):
		print('Restaurant does business')

	def set_number_served(self ,num):
		self.number_served = num

	def increment_number_served(self,num):
		while self.number_served

'''
new_Restaurant.py
'''

'''
9-4 就餐人数:在为完成练习 9-1 而编写的程序中,添加一个名为 number_served
的属性,并将其默认值设置为 0。根据这个类创建一个名为 restaurant 的实例;打印有
多少人在这家餐馆就餐过,然后修改这个值并再次打印它。
添加一个名为 set_number_served()的方法,它让你能够设置就餐人数。调用这个
方法并向它传递一个值,然后再次打印这个值。
添加一个名为 increment_number_served()的方法,它让你能够将就餐人数递增。
调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。
'''
class Restaurant():
	'''
	#这种当然也可以
	def __init__(self,restaurant_name,cuisine_type):
		self.restaurant_name = restaurant_name
		self.cuisine_type = cuisine_type
		self.number_served = 0
	'''
	def __init__(self,restaurant_name,cuisine_type,number_served =0):
		self.restaurant_name = restaurant_name
		self.cuisine_type = cuisine_type
		self.number_served = number_served

	def describe_restaurant(self):
		print('restaurant_name is '+self.restaurant_name+'\n'+'cuisine_type is '+self.cuisine_type)

	def open_restaurant(self):
		print('Restaurant does business')

	def set_number_served(self ,num):
		self.number_served = num

	def increment_number_served(self,num):
		while self.number_served

'''
User.py
'''


class User():
	def __init__(self,first_name,last_name,idNumber,login_attempts = 0,location = 'local'):
		self.first_name = first_name
		self.last_name = last_name
		self.id = idNumber
		self.location = location
		self.login_attempts = login_attempts

	def describe_user(self):
		print('first_name : '+self.first_name+', last_name : '+self.last_name+ ' \nid : '+self.id+' \nlocation : '+self.location)
		print('login_attempts is '+str(self.login_attempts))

		

	def greet_user(self):
		print('hello, how are you, '+self.first_name+'.'+self.last_name+'?')
		print('your id is '+self.id+' , please remenber it.')
		print(self.location+' is a nice place!')
		print('login_attempts is '+str(self.login_attempts))

	def increment_login_attempts(self):
		self.login_attempts += 1

	def reset_login_attempts(self):
		self.login_attempts = 0

你可能感兴趣的:(python从入门到实践第九章的练习题作业)