栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python编程:从入门到实践 练习答案 Chapter11

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python编程:从入门到实践 练习答案 Chapter11

20211107

11-1
city_functions.py
def get_city_country(city, country):
    return city.title() + ', ' + country.title()


test_cities.py
import unittest
from city_functions import get_city_country

class CityTestCase(unittest.TestCase):    
    def test_get_city_country(self):
        formatted_city = get_city_country('changsha', 'china')
        self.assertEqual(formatted_city, 'Changsha, China')
unittest.main()

11-2
 city_functions.py
 def get_city_country(city, country, population=''):
    if population:
        return city.title() + ', ' + country.title() + 
            ' -population ' + population
    else:
        return city.title() + ', ' + country.title()


test_cities.py
import unittest
from city_functions import get_city_country

class CityTestCase(unittest.TestCase):  
    def test_get_city_country(self):
        formatted_city = get_city_country('changsha', 'china')
        self.assertEqual(formatted_city, 'Changsha, China') 
               
    def test_get_city_country_population(self):
        formatted_city = get_city_country('changsha', 'china', '12966836')
        self.assertEqual(formatted_city, 
                         'Changsha, China -population 12966836')
unittest.main()        

11-3
empoy.py
class Employee():
    def __init__(self, givenname, surname, salary):
        self.givenname = givenname
        self.surname = surname
        self.salary = salary
        
    def give_raise(self, add_salary = 5000):
        self.salary += add_salary
        return self.salary   #---------------don't missed    


testemploy.py
import unittest
from employ import Employee

class TestEmployee(unittest.TestCase):
    def setUp(self):
        self.emp1 = Employee('g', 's', 1000000)
        
    def test_give_default_raise(self):
        raised_salary = self.emp1.give_raise()
        self.assertEqual(raised_salary, 1005000)
              
    def test_give_custom_raise(self):
        raised_salary = self.emp1.give_raise(10000)
        self.assertEqual(raised_salary, 1010000)
        
unittest.main()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/444457.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号