python程式設計從入門到實踐 答案 第十一章

2021-10-02 17:09:56 字數 4199 閱讀 4718

11-1 城市和國家

1

. city _functions.py:

defget_city_country_name

(city_name,country_name)

:"""返回city, country格式"""

full_name = city_name.title()+

', '

+ country_name.title(

)return full_name

2. test_cities.py

import unittest

from city_functions import get_city_country_name

class

nametestcase

(unittest.testcase)

:"""測試city_functions.py"""

deftest_city_country

(self)

:"""能夠正確處理像beijing, china這樣的?"""

formatted_name = get_city_country_name(

'beijing'

,'china'

) self.assertequal(formatted_name,

'beijing, china'

)#equal: unittest.main

if __name__ ==

'__main__'

: unittest

11-2 人口數量

2. population設定為可選的

city_functions.py

defget_city_country_name

(city_name,country_name,population='')

:"""返回city, country格式"""

if population:

full_name = city_name.title()+

', '

+ country_name.title()+

' - population '

+str

(population)

else

: full_name = city_name.title()+

', '

+ country_name.title(

)return full_name

test_cities.py

import unittest

from city_functions import get_city_country_name

class

nametestcase

(unittest.testcase)

:"""測試city_functions.py"""

deftest_city_country

(self)

:"""能夠正確處理像beijing, china這樣的?"""

formatted_name = get_city_country_name(

'beijing'

,'china'

) self.assertequal(formatted_name,

'beijing, china'

)#equal: unittest.main

if __name__ ==

'__main__'

: unittest

再編寫乙個名為 test_city_country_population()的測試,

1.city_functions.py

defget_city_country_name

(city_name,country_name,population='')

:"""返回city, country格式"""

if population:

full_name = city_name.title()+

', '

+ country_name.title()+

' - population '

+str

(population)

else

: full_name = city_name.title()+

', '

+ country_name.title(

)return full_name

2.test_cities.py

import unittest

from city_functions import get_city_country_name

class

nametestcase

(unittest.testcase)

:"""測試city_functions.py"""

deftest_city_country

(self)

:"""能夠正確處理像beijing, china這樣的?"""

formatted_name = get_city_country_name(

'beijing'

,'china'

) self.assertequal(formatted_name,

'beijing, china'

)def

test_city_country_population

(self)

:"""測試加上population"""

formatted_name = get_city_country_name(

'beijing'

,'china'

,50000

) self.assertequal(formatted_name,

'beijing, china - population 50000'

)#equal: unittest.main

if __name__ ==

'__main__'

: unittest

11-3 雇員

employee.py

# -*- coding: utf-8 -*-

class

employee()

:def

__init__

(self,f_name,l_name,salary_year)

: self.f_name = f_name

self.l_name = l_name

self.salary_year = salary_year

defgive_raise

(self,amount=

5000):

self.salary_year += amount

test_employee.py

# -*- coding: utf-8 -*-

import unittest

from employee import employee

class

testemployee

(unittest.testcase)

:def

setup

(self)

:"""建立乙個雇員例項"""

self.x** = employee(

'x',

'j',

5000

)def

test_give_default_raise

(self)

:"""測試預設"""

self.x**.give_raise(

) self.assertequal(self.x**.salary_year,

10000

)def

test_give_custom_raise

(self)

:"""測試隨意生成"""

self.x**.give_raise(

1000

) self.assertequal(self.x**.salary_year,

6000

)if __name__ ==

'__main__'

: unittest

Python 程式設計 從入門到實踐

1.官網安裝 3.環境配置 務必選中核取方塊add python to path 4.檢視 啟動python版本的命令 python 執行 print hello python world 5.終端執行x.py檔案 python x.py 7.檢視當前目錄中的所有檔案的命令 dir windows系...

Python程式設計從入門到實踐 基礎入門

python程式設計從入門到實踐 基礎入門 1 python中的變數 2 python首字母大寫使用title 方法,全部大寫upper 方法,全部小寫lower 方法 3 python中字串拼接使用 號 4 python中刪除字串的空格 刪除末尾空格的rstrip 刪除開頭空格的lstrip 刪除...

Python程式設計 從入門到實踐 1

內容總結自 python程式設計 從入門到實踐 安裝python3 安裝文字編輯器sublime text並配置python3環境 安裝sublime text tools new build system 將 untitled.sublime build 文件中的所有內容刪除,輸入以下內容 注意,...