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

习题11-1&11-2 
name_function.py
#!/usr/bin/env	python
#-*-coding:utf8-*-
def format_name (first,last,middle=''):
	""" 格式化一个姓名"""
	
	if middle:
		fullname = first +' '+middle+' '+last
	else:
		fullname = first +' '+last
	return fullname.title()
def format_city (city,country,population=''):
	""" 格式化一个国家和城市 """
	if population:
		city_country = city +','+country+' - population'+' '+population
	else:                      
		city_country = city +','+country
	return city_country.title()
		
test_name_function.py
#!/usr/bin/env	python
#-*-condig:utf8-*-
import unittest
from name_function import format_name
from name_function import format_city
class NameTestCase(unittest.TestCase):
	""" 测试name_function.py """
	def test_first_last_name (self):
		""" 能够正确的处理像james hardon这样的姓名吗? """
		formattedname = format_name('james','hardon')
		self.assertEqual(formattedname,'James Hardon')
	def test_first_middle_last_name (self):
		""" 能够正确的处理像james hardon ding这样的姓名吗?"""
		formattedname = format_name('james','hardon','ding')
		self.assertEqual(formattedname,'James Ding Hardon')
	def	test_city_country(self):
		"""能够正确的处理像santiago,chile"""
		formattedcity = format_city('santiago','chile')
		self.assertEqual(formattedcity,'Santiago,Chile')
	def test_city_country_population (self):
		""" 能够处理带人口的城市吗? """
		formattedcity = format_city('santiago','chile',population='500000')
		self.assertEqual(formattedcity,'Santiago,Chile - Population 500000')
unittest.main()
习题11-3
Employee.py
#!/usr/bin/env	python
#-*-coding:utf8-*-
class Employee:
	""" 收集雇员信息 """
	def __init__ (self,first,last,salary):
		""" 雇员信息 """
		self.first = first
		self.last = last
		self.salary = salary
	def give_raise (self,addsalary=5000):
		""" Function doc """
		self.salary+=addsalary
		
test_Employee.py
#!/usr/bin/env	python
#-*-coding:utf8-*-
import unittest
from Employee import Employee
class EmployeeTestCase(unittest.TestCase):
	""" 测试不同的雇员信息 """

	def setUp (self):
		""" 初始化雇员信息 """
		self.formatted_default = Employee('ding','yuanxue',1000)
	def test_give_default_raise (self):
		""" 默认加薪 """
		self.formatted_default.give_raise()
		self.assertEqual(self.formatted_default.salary,6000)
	def test_give_coustom_raise (self):
		"""特殊加薪"""
		self.formatted_default.give_raise(8000)
		self.assertEqual(self.formatted_default.salary,9000)	
	
unittest.main()




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值