高级编程技术 课后作业六

6-1 人:使用一个字典来存储一个熟人的信息。
acquaintance = {
    'first_name': 'Black',
    'last_name': 'Smith',
    'age': '23',
    'city': 'America',
    }
	
print(acquaintance)
{'first_name': 'Black', 'last_name': 'Smith', 'age': '23', 'city': 'America'}

6-2 喜欢的数字:使用一个字典来存储一些人喜欢的数字
favorite_number = {"Aabbye":3,"Babara":1,"Cady":4,"Dafydd":1,"Eager":5}

print(favorite_number)
{'Aabbye': 3, 'Babara': 1, 'Cady': 4, 'Dafydd': 1, 'Eager': 5}


6-4 词汇表2 :既然你知道了如何遍历字典,现在请整理你为完成练习6-3而编写的代码,将其中的一系列print 语句替换为一个遍历字典中的键和值的循环。确定该循环正确无误后,再在词汇表中添加5个Python术语。当你再次运行这个程序时,这些新术语及其含义将自动包含在输出中。
words = {
    'abs':'absolute',
    'arg':'argument',
    'buf':'buffer',
    'ctrl':'control',
    'del':'delete',
    'argument':'实参',
    'attribute':'属性',
    'base class':'基本类',
    'block':'块',
    'character':'字符',
    }
    
for key,value in words.items():
    print(key + ':' + value)
abs:absolute
arg:argument
buf:buffer
ctrl:control
del:delete
argument:实参
attribute:属性
base class:基本类
block:块
character:字符

6-5 河流 :创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键—值对可能是'nile': 'egypt' 。

· 使用循环为每条河流打印一条消息,如“The Nile runs throughEgypt.”。

· 使用循环将该字典中每条河流的名字都打印出来。

· 使用循环将该字典包含的每个国家的名字都打印出来。

rivers = {
    'nile':'egypt',
    'amazon':'brazil',
    'the yangtze river':'china'
    }
	
for key,value in rivers.items():
    print('The ' + key.title() + 'runs through ' + value.title() + '.')

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

print()	
for value in rivers.values():
    print(value.title())
The Nileruns through Egypt.
The Amazonruns through Brazil.
The The Yangtze Riverruns through China.

Nile
Amazon
The Yangtze River

Egypt
Brazil
China


6-6 调查 :在6.3.1节编写的程序favorite_languages.py中执行以下操作。
· 创建一个应该会接受调查的人员名单,其中有些人已包含在字典中,而其他人未包含在字典中。
· 遍历这个人员名单,对于已参与调查的人,打印一条消息表示感谢。对于还未参与调查的人,打印一条消息邀请他参与调查。
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
    }

peoples = ['jen','jack','edward','micheal','phil','lucy']

for people in peoples: 
    if people in favorite_languages.keys():
	print(people + ',thank you for participating in our survey.')
    else:
	print(people + ',would you like to participate in our survey?')
jen,thank you for participating in our survey.
jack,would you like to participate in our survey?
edward,thank you for participating in our survey.
micheal,would you like to participate in our survey?
phil,thank you for participating in our survey.
lucy,would you like to participate in our survey?

6-7 人 :在为完成练习6-1而编写的程序中,再创建两个表示人的字典,然后将这三个字典都存储在一个名为people 的列表中。遍历这个列表,将其中每个人的所有信息都打印出。
people1 = {'first_name': 'Black','last_name': 'Smith','age': '23','city': 'America'}
people2 = {'first_name': 'Donald','last_name': 'Trump','age': '72','city': 'America'}
people3 = {'first_name': 'Theresa','last_name': 'May','age': '62','city': 'Britain'}

peoples = [people1,people2,people3]

for people in peoples:
    print(people)
{'first_name': 'Black', 'last_name': 'Smith', 'age': '23', 'city': 'America'}
{'first_name': 'Donald', 'last_name': 'Trump', 'age': '72', 'city': 'America'}
{'first_name': 'Theresa', 'last_name': 'May', 'age': '62', 'city': 'Britain'}

6-8 宠物 :创建多个字典,对于每个字典,都使用一个宠物的名称来给它命名;在每个字典中,包含宠物的类型及其主人的名字。将这些字典存储在一个名为pets 的列表中,再遍历该列表,并将宠物的所有信息都打印出来。
Michelle = {'species':'dog','owner':'Micheal'}
Gabrielle = {'species':'cat','owner':'Smith'}
Rafael = {'species':'bird','owner':'Lucy'}

pets = [Michelle,Gabrielle,Rafael]

for pet in pets:
    print(pet)
{'species': 'dog', 'owner': 'Micheal'}
{'species': 'cat', 'owner': 'Smith'}
{'species': 'bird', 'owner': 'Lucy'}


6-9 喜欢的地方 :创建一个名为favorite_places 的字典。在这个字典中,将三个人的名字用作键;对于其中的每个人,都存储他喜欢的1~3个地方。为让这个练习更有趣些,可让一些朋友指出他们喜欢的几个地方。遍历这个字典,并将其中每个人的名字及其喜欢的地方打印出来。
favorite_places = {
    'Smith':['Amecica'],
    'Jack':['India','Japan'],
    'LiMing':['China','France','Britan']
    }
	
for people,favorite_place in favorite_places.items():
    print(people + "'s favorite places are ",end = '')
    print(favorite_place,end ='.\n')
Smith's favorite places are ['Amecica'].
Jack's favorite places are ['India', 'Japan'].
LiMing's favorite places are ['China', 'France', 'Britan'].


6-10 喜欢的数字 :修改为完成练习6-2而编写的程序,让每个人都可以有多个喜欢的数字,然后将每个人的名字及其喜欢的数字打印出来。
favorite_numbers = {
    "Aabbye":[1,2,3,4,5],
    "Babara":[2,4,6,8,10],
    "Cady":[3,6,9,12,15],
    "Dafydd":[4,8,12,16,20],
    "Eager":[5,10,15,20,25]
    }

for people,favorite_number in favorite_numbers.items():
    print(people + "'s favorite numbers are ",end = '')
    print(favorite_number,end ='.\n')
Aabbye's favorite numbers are [1, 2, 3, 4, 5].
Babara's favorite numbers are [2, 4, 6, 8, 10].
Cady's favorite numbers are [3, 6, 9, 12, 15].
Dafydd's favorite numbers are [4, 8, 12, 16, 20].
Eager's favorite numbers are [5, 10, 15, 20, 25].


6-11 城市 :创建一个名为cities 的字典,其中将三个城市名用作键;对于每座城市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该城市的事实。在表示每座城市的字典中,应包含country 、population 和fact 等键。将每座城市的名字以及有关它们的信息都打印出来。
cities = {
    'Beijing':{
	'country':'China',
	'population':'21 billion',
	'fact':'Chinese Captial'
	},
    'Washington DC':{
	'country':'the United States',
	'population':'550 thousand',
	'fact':'American Captial'
	},
    'London':{
	'country':'Britan',
	'population':'8 billion',
	'fact':'British Captial'
	}
    }
	
for city_name,city_info in cities.items():
    print("City's name:\n" + city_name + "\nCity's infomation:")
    print(city_info)
    print()
City's name:
Beijing
City's infomation:
{'country': 'China', 'population': '21 billion', 'fact': 'Chinese Captial'}

City's name:
Washington DC
City's infomation:
{'country': 'the United States', 'population': '550 thousand', 'fact': 'American Captial'}

City's name:
London
City's infomation:
{'country': 'Britan', 'population': '8 billion', 'fact': 'British Captial'}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值