第十一章課後習題

2021-08-18 17:35:02 字數 3855 閱讀 8108

11-1城市和國家:編寫乙個函式,它接受兩個形參:乙個城市名和乙個國家名。這個函式返回乙個格式為 city, country 的字串,如 santiago, chile。將這個函式儲存在乙個名為 city _functions.py 的模組中。建立乙個名為 test_cities.py 的程式,對剛編寫的函式進行測試(別忘了,你需要匯入模組 unittest 以及要測試的函式)。編寫乙個名為 test_city_country()的方法,核實使用類似於'santiago'和'chile'這樣的值來呼叫前述函式時,得到的字串是正確的。執行 test_cities.py,確認測試 test_city_country()通過了。

city_functions.py

def cityname(city,country,population):

format_name=city.title()+','+country.title()

return format_name

test_cities.py

from city_functions import *

import unittest

class citytest(unittest.testcase):

"""docstring for citytest"""

def test_city_country(self):

format_name=cityname('guangzhou','china')

self.assertequal(format_name,'guangzhou,china')

unittest.main()

執行結果:

11-2人口數量:修改前面的函式,使其包含第三個必不可少的形參 population,並

返回乙個格式為 city, country – population *** 的字串,如 santiago, chile –

population 5000000。執行 test_cities.py,確認測試 test_city_country()未通過。

city_functions.py

def cityname(city,country,population):

format_name=city.title()+','+country.title()+'-'+"population "+str(population)

return format_name

執行結果:

修改上述函式,將形參 population 設定為可選的。再次執行 test_cities.py,確認測

試 test_city_country()又通過了。

city_functions.py

def cityname(city,country,population=''):

format_name=city.title()+','+country.title()

if population:

format_name+=("population "+str(population))

return format_name

執行結果:

再編寫乙個名為 test_city_country_population()的測試,核實可以使用類似於

'santiago'、 'chile'和'population=5000000'這樣的值來呼叫這個函式。再次執行

test_cities.py,確認測試 test_city_country_population()通過了。

from city_functions import *

import unittest

class citytest(unittest.testcase):

"""docstring for citytest"""

def test_city_country(self):

format_name=cityname('guangzhou','china')

self.assertequal(format_name,'guangzhou,china')

def test_city_country_population(self):

format_name=cityname('guangzhou','china',500000)

self.assertequal(format_name,'guangzhou,china-population 500000')

unittest.main()

執行結果:

11-3雇員:編寫乙個名為 employee 的類,其方法__init__()接受名、姓和年薪,並

將它們都儲存在屬性中。編寫乙個名為 give_raise()的方法,它預設將年薪增加 5000

美元,但也能夠接受其他的年薪增加量。

為 employee 編寫乙個測試用例,其中包含兩個測試方法: test_give_default_

raise()和 test_give_custom_raise()。使用方法 setup(),以免在每個測試方法中都創

建新的雇員例項。執行這個測試用例,確認兩個測試都通過了。

import unittest

class employee(object):

"""docstring for employee"""

def __init__(self, first, last, ymon):

self.first=first

self.last=last

self.ymon=ymon

def give_raise(self,increase=5000):

self.ymon+=increase

class testemployee(unittest.testcase):

"""docstring for testemployee"""

def setup(self):

self.employee=employee('ke','sibo',1000)

def test_give_default_raise(self):

self.employee.give_raise()

self.assertequal(self.employee.ymon,6000)

def test_give_custom_raise(self):

self.employee.give_raise(2000)

self.assertequal(self.employee.ymon,3000)

unittest.main()

執行結果:

《鳥哥linux》 第十一章課後習題答案

1.在linux上可以找到哪些shell?哪個檔案記錄可用的shell?兒linux預設的shell是?1.bin bash,bin tcsh,bin csh 2.etc shells 3.bash,亦即是 bin bash 2.在shell環境下,有個提示符 prompt 他可以修改嗎?要改什麼?...

第十一章 正規表示式課後習題

1.把 etc passwd 複製到 root test.txt,用sed列印所有行。root zl cloud cp etc passwd root test.txt root zl cloud sed n 1,p test.txt root zl cloud 2.列印test.txt的第3行 第...

《Python從入門到實踐》 課後習題第十一章

11 1 城市和國家 編寫乙個函式,它接受兩個形參 乙個城市名和乙個國家名。這個函式返回乙個格式為city,country 的字串,如santiago,chile 將 這個函式儲存在乙個名為city functions.py的模組中。建立乙個名為test cities.py的程式,對剛編寫的函式進行...