您的位置:首页 > 编程语言 > Python开发

Python第三周第二次作业

2018-03-21 23:33 375 查看
本章学习字典的使用
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 = '')

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: