Python第三周第二次作业

本章学习字典的使用

6-3 词汇表

alphabet = {
    'cin': 'to input some datas',
    'cout': 'to output some datas',
    'print': 'to output some datas',
    'upper': 'to make letter become upper',
    'lower': 'to make letter become lower',
    }
for k, v in alphabet.items():
    print(k + ': ' + v)

6-5 河流

先打印键与值,再分别打印键和值(只有打印键时可以省略key())

rivers = {
    'yellowRiver': 'China',
    'nile': 'Egypt',
    'mississipi': 'America',
    }
for river, country in rivers.items():
    print('The ' + river.title() + ' runs through ' + country + '.')
for river in rivers:
    print(river)
for country in rivers.values():
    print(country)

6-8 宠物

似乎不能将声明的变量直接当成字符串打印出来?如果可以就好了……

meow = {'name': 'meow', 'kind': 'cat', 'master': 'Alice'}
woof = {'name': 'woof', 'kind': 'dog', 'master': 'Bob'}
tweet = {'name': 'tweet', 'kind': 'bird', 'master': 'Cathy'}
pets = [meow, woof, tweet]
for pet in pets:
    print(pet['name'].title() + ' is a ' + 
    pet['kind'] + ', its master is ' + 
    pet['master'] + '.')

6-11 城市

做这一题时我才发现:之前写print('\n')会变成换两行,是因为print默认带有换行……

print('\n', end = '')也可以直接写成print('')

cities = {
    'Guangzhou': {'country': 'China', 'population': 15, 'fact': 'Beautiful!'}, 
    'London': {'country': 'England', 'population': 8, 'fact': 'Wonderful!'}, 
    'NewYork': {'country': 'America', 'population': 12, 'fact': 'Amazing!'}, 
    }
for city, message in cities.items():
    print(city + '---', end = '')
    for key, value in message.items():
        print(key + ': ' + str(value) + '   ', end = '')
    print('\n', end = '')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值