您的位置:首页 > 其它

第三周作业——第六章动手试一试

2018-03-22 18:00 295 查看
6-1 人 人 :使用一个字典来存储一个熟人的信息,包括名、姓、年龄和居住的城市。该字典应包含键first_name 、last_name 、age 和city 。将存储在该字典中 的每项信息都打印出来。
friend = {'first_name':'Xu','last_name':'Su','age':20,'city':'Chaozhou'}
print(friend['first_name']+'\n')
print(friend['last_name']+'\n')
print(str(friend['age'])+'\n')
print(friend['city']+'\n')
 6-5 河流 河流 :创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键—值对可能是'nile': 'egypt' 。 使用循环为每条河流打印一条消息,如“The Nile runs through Egypt.”。
使用循环将该字典中每条河流的名字都打印出来。
使用循环将该字典包含的每个国家的名字都打印出来。
rivers = {
'nile': 'egypt',
'Yangtze River':'China',
'Yellow River':'China'
}
for river_name,city_name in rivers.items():
print('The '+river_name.title()+' runs through '+city_name.title())

for river_name in rivers.keys():
print(river_name.title())

for city_name in rivers.values():
print(city_name.title())


6-7 人 人 :在为完成练习6-1而编写的程序中,再创建两个表示人的字典,然后将这三个字典都存储在一个名为people 的列表中。遍历这个列表,将其中每个人的所有 信息都打印出来。
friend1 = {'first_name':'Xu','last_name':'Su','age':'20','city':'Chaozhou'}
friend2 = {'first_name':'Si','last_name':'Hu','age':'21','city':'Guangzhou'}
friend3 = {'first_name':'Ze','last_name':'Yi','age':'19','city':'Shantou'}
people = [friend1,friend2,friend3]
for friend in people:
print(friend)

 6-11 城市 城市 :创建一个名为cities 的字典,其中将三个城市名用作键;对于每座城市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该 城市的事实。在表示每座城市的字典中,应包含country 、population 和fact 等键。将每座城市的名字以及有关它们的信息都打印出来。
cities = {
'Guangzhou':{
'country':'China',
'population':'1449.84 million',
'fact':'in Guangdong',
},
'Beijing':{
'country':'China',
'population':'2172 million',
'fact':'Capital of China',
},
'New York':{
'country':'the USA',
'population':'1000 million',
'fact':'Capital of American',
}
}
for city_name,city_inf in cities.items():
print(city_name+':     country:'+city_inf['country']
+'     population:'+city_inf['population']+'     fact:'+city_inf['fact'])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: