Python编程:从入门到实践第9章习题答案

print('----9-1习题答案----')
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 Name is "+self.restaurant_name)
		print	("The Type is "+self.cuisine_type)
	def open_restaurant(self):
		print('In Operate~')
		
restaurant=Restaurant('东北水饺','快餐')	

print(restaurant.restaurant_name+' '+restaurant.cuisine_type)

restaurant.open_restaurant()
restaurant.describe_restaurant()
print('---9-2习题答案---')

restaurant1=Restaurant('盐帮菜','中餐')
restaurant2=Restaurant('巴蜀崽','火锅')
restaurant3=Restaurant('IceCream','西餐')
restaurant1.describe_restaurant()
restaurant2.describe_restaurant()
restaurant3.describe_restaurant()
print('----9-3习题答案:----')

class User():
	'''basic user'''
	def __init__ (self,first_name,last_name):
		self.first_name=first_name
		self.last_name=last_name
	def describe_user (self):
		print('The first name is '+self.first_name.title())
		print('The last name is '+self.last_name.title())
	def greet_user(self):
		full_name=self.first_name+' '+self.last_name
		print('Hello '+full_name.title())
my_user1=User('jack','json')
my_user2=User('LeBron','james')
my_user3=User('james','hardon')
my_user1.describe_user()
my_user1.greet_user()
my_user2.describe_user()
my_user2.greet_user()
my_user3.describe_user()
my_user3.greet_user()		
print('----9-4----习题答案')
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 Name is "+self.restaurant_name)
		print	("The Type is "+self.cuisine_type)
		print	("There is "+str(self.number_served))
	def open_restaurant(self):
		print('In Operate~')
	def set_number_served (self,number_served):
		""" Function doc """
		self.number_served=number_served
	def increment_number_served (self,increment):
		""" Function doc """
		self.number_served=+increment
		
		
		
My_restaurant=Restaurant('巴蜀崽','火锅')
My_restaurant.describe_restaurant()
My_restaurant.number_served=10
My_restaurant.describe_restaurant()
My_restaurant.set_number_served(20)
My_restaurant.describe_restaurant()
My_restaurant.increment_number_served(50)
My_restaurant.describe_restaurant()
print('----9-5习题答案----')
class User():
	'''basic 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 first name is '+self.first_name.title())
		print('The last name is '+self.last_name.title())
	def greet_user(self):
		full_name=self.first_name+' '+self.last_name
		print('Hello '+full_name.title())
	def increment_login_attempts (self):
		""" Function doc """
		self.login_attempts+=1
	def reset_login_attempts (self):
		""" Function doc """
		self.login_attempts=0
My_user=User('ding','yuaxue')
My_user.describe_user()
My_user.increment_login_attempts()
My_user.increment_login_attempts()
My_user.increment_login_attempts()
print(My_user.login_attempts)	
My_user.reset_login_attempts()
print(My_user.login_attempts)
print('----9-6习题答案----')
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 Name is "+self.restaurant_name)
		print	("The Type is "+self.cuisine_type)
	def open_restaurant(self):
		print('In Operate~')
class IceCreamStand(Restaurant):
	def __init__ (self,restaurant_name,cuisine_type):
		""" Function doc """
		super().__init__(restaurant_name,cuisine_type)
		self.flavors=['one','two','three']
	def flavor (self):
		""" Function doc """
		for flavor in self.flavors:
			print(flavor)
My_flavor =	IceCreamStand('避风塘','饮料')
My_flavor.flavor()
print('----9-7习题答案----')
class User():
	'''basic 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 first name is '+self.first_name.title())
		print('The last name is '+self.last_name.title())
	def greet_user(self):
		full_name=self.first_name+' '+self.last_name
		print('Hello '+full_name.title())
	def increment_login_attempts (self):
		""" Function doc """
		self.login_attempts+=1
	def reset_login_attempts (self):
		""" Function doc """
		self.login_attempts=0			
class Admin(User):
	""" Class doc """
	def __init__ (self,first_name,last_name):
		""" Function doc """
		super().__init__(first_name,last_name)
		self.privileges=['can add post','can delete post','can ban user']
	def show_privileges (self):
		""" Function doc """
		print(self.privileges)
My_admin =	Admin('ding','yuanxue')
My_admin.greet_user()
My_admin.show_privileges()	

print('----9-8习题答案----')
class Privileges():
	""" Class doc """
	def __init__ (self):
		""" Function doc """
		self.privileges = ['can add post','can delete post','can ban user']
	def show_privileges (self):
		""" Function doc """
		print(self.privileges)	
class User():
	'''basic 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 first name is '+self.first_name.title())
		print('The last name is '+self.last_name.title())
	def greet_user(self):
		full_name=self.first_name+' '+self.last_name
		print('Hello '+full_name.title())
	def increment_login_attempts (self):
		""" Function doc """
		self.login_attempts+=1
	def reset_login_attempts (self):
		""" Function doc """
		self.login_attempts=0			
class Admin(User):
	""" Class doc """
	def __init__ (self,first_name,last_name):
		""" Function doc """
		super().__init__(first_name,last_name)
		self.privileges = Privileges()
My_admin1=Admin('ding','yuanxue')
My_admin1.privileges.show_privileges()
print('----9-9习题答案----')
class Car():
	""" Class doc """
	def __init__ (self,make,model,year):
		self.make =	make
		self.model = model
		self.year =	year
		self.odometer_recoding = 0
class EletricCar(Car):
	def __init__ (self,make,model,year):
		super().__init__(make,model,year)
		self.battery=Battery()
class Battery():
	def __init__ (self,battery_size=70):
		""" Function doc """
		self.battery_size = battery_size
	def upgrade_battery (self):
		""" Function doc """
		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)
			
			
My_Car =	EletricCar('tsla','model s',2016)
My_Car.battery.get_range()
My_Car.battery.upgrade_battery()
My_Car.battery.get_range()	
print('----9-10~9-12忽略----')
print('----9-13习题答案----')

from collections import OrderedDict
#favrate_languages = OrderedDict()
favrate_languages = {}
favrate_languages['jack'] = 'python'
favrate_languages['james'] = 'java'
favrate_languages['ding'] =	'c++'
for name,language in favrate_languages.items():
	print(name.title()+"'s favrate language is "+language.title())
print('----9-14-----习题答案')
from random import randint
class Die():
	""" Class doc """
	def __init__ (self):
		""" Function doc """
		self.sides = 6
	def roll_die (self):
		""" Function doc """
		x = randint(1,self.sides)
		print(str(x))
six = Die()	
print('----6面,掷10次----')
for i in range(0,10):
	six.roll_die()	
print('----10面,掷10次----')
six.sides=10
for i in range(0,10):
	six.roll_die()
print('----20面,掷10次----')
six.sides=20
for i in range(0,10):
	six.roll_die()



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值