python编程从入门到实践练习11-2:人口数量

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}.')
        


简单程序输出结果
测试结果!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyyxx1990

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值