python编程从入门到实践练习11-2:人口数量
import unittest
from city_functions import get_city_country
class NameTestCase(unittest.TestCase):
'''测试city_function.py'''
def test_city_country(self):
'''能够正确处理像Santiago, Chile这样格式的输出结果吗?'''
formatted_name = get_city_country('santiago', 'chile')
self.assertEqual(formatted_name,'Santiago, Chile')
def test_city_country_population(self):
'''
能够正确处理像Santiago, Chile - population 5000000
这样的输出结果吗?
'''
formatted_name = get_city_country('santiago', 'chile', '5000000')
self.assertEqual(formatted_name,'Santiago, Chile - Population 5000000')
if __name__ == '__main__':
unittest.main()
> 这里是引用模块city_function.py文件的代码
> def get_city_country(city,country,population=''):
'''生成格式为city,country的字符串'''
if population:
full_name = (f'{city}, {country} - population {population}')
else:
full_name = f'{city}, {country}'
return full_name.title()
>
> 这里是引用简单程序尝试输出的代码
> from city_functions import get_city_country
print('Enter "q" at any time to quit.')
while True:
city = input('\nPlease enter the city name:')
if city == 'q':
break
country = input('\nPlease enter the country name:')
if country == 'q':
break
population = input('\nPlease enter the population number:')
if population == 'q':
break
formatted_name = get_city_country(city,country,population)
print(f'\tThe city-country-population is : {formatted_name}.')