python从⼊门到实践第六章答案-《Python编程:从⼊门到实
践》第6章习题
#6-1 ⼈:使⽤⼀个字典来存储⼀个熟⼈的信息,包括名、姓、年龄和居住的城市。该字典应
#包含键first_name、last_name、age和city。将存储在该字典中的每项信息都打印出来。
informations = {
"first_name":"wang",
python编程:从入门到实践第二版"last_name":"weizhong",
"city":"Guangyuan"
}
print(informations)
#6-2 喜欢的数字:使⽤⼀个字典来存储⼀些⼈喜欢的数字。请想出5个⼈的名字,并将这些名字
#⽤作字典中的键;想出每个⼈喜欢的⼀个数字,并将这些数字作为值存储在字典中。
#打印每个⼈的名字和喜欢的数字。为让这个程序更有趣,通过询问朋友确保数据是真实的。
favourite_number = {
"wwz":"8",
"fp":"7",
"lmh":"6",
"aaa":"5",
"bbb":"4"
}
print(favourite_number)
print("www喜欢的数字是:" + str(favourite_number["wwz"]) + ".")
#6-3 词汇表:Python字典可⽤于模拟现实⽣活中的字典,但为避免混淆,我们将后者
#称为词汇表。
word_implication = {
"print":"显⽰",
"upper":"全部⼤写",
"title":"冠字",
"if":"如果",
"input":"显⽰并输⼊"}
for word in word_implication:
print(word + " 的含义是:" + word_implication[word])
#6-4 词汇表2:既然你知道了如何遍历字典,现在请整理你为完成练习6-3⽽编写的代码,
#将其中的⼀系列print语句替换为⼀个遍历字典中的键和值的循环。确定该循环正确⽆#误后,再在词汇表中添加5个Python术语。当你再次运⾏这个程序时,这些新术语及其#含义将⾃动包含在输出中。
word_implication = {
"print":"显⽰",
"upper":"全部⼤写",
"title":"冠字",
"if":"如果",
"input":"显⽰并输⼊"
}
print("键的循环:")
for key in word_implication.keys():
print(key)
print("值的循环:")
for values in word_implication.values():
print(values)
#6-5 河流:创建⼀个字典,在其中存储三条⼤河流及其流经的国家。其中⼀个(键—值) #对可能是"nile":"egypt"。
river_country = {
"Nile":"Egypt",
"Mississippi":"USA",
"Danube":"Germany"
}
for key , values in river_country.items():
print("The " + key.title() + " runs through " + values.title() + ".")
for key_river in river_country.keys():
print(key_river.title())
for values_river in river_country.values():
print(values_river.title())
#6-6 调查:在6.3.1节编写的程序favorite_languages.py中执⾏以下操作。
favorite_languages = {
"jen":"python",
"sarah":"c",
"edward":"ruby",
"phil":"python",
}
people_research = ["sarah","phil"]
for name in favorite_languages.keys():
if name in people_research:
print(name.title() + ",感谢您参加我们的这次关于编程语⾔的调查!")
else:
print("亲爱的 " + name.title() + ",我们希望你能参与我们的这次调查!")
#6-7 ⼈:在为完成练习6-1⽽编写的程序中,再创建两个表⽰⼈的字典,然后将这三个字典#都存储在⼀个名为people的列表中。遍历这个列表,将其中每个⼈的所有信息都打印出来。people = {
"name_1":{
"first_name":"wang",
"last_name":"weizhong",
"city":"guangyuan"
},
"name_2":{
"first_name":"fan",
"last_name":"peng",
"city":"xuefeng"
}
}
for username , user_inof in people.items():
print("⽤户:" + username)
full_name = user_inof["first_name"] +""+ user_inof["last_name"]
city = user_inof["city"]
print("全名:" + full_name.title() + " ;住址:" + city.title())
#6-8宠物:创建多个字典,对于每个字典,都使⽤⼀个宠物的名称来给它命名;在每个字
#典中,包含宠物的类型及其主⼈的名字。 将这些字典存储在⼀个名为pets的列表中,再遍#历该列表,并将宠物的所有信息都打印出来。
pets = {
"pumpkin":{"variety":"cat","master":"mwm"},
"huanhuan":{"variety":"dog","master":"psp"}
}
for key , value in pets.items():
print(key.title() + "是只" + value["variety"].title() +
",它主⼈叫做 " + value["master"].title())
#6-9喜欢的地⽅:创建⼀个名为favorite_places的字典。在这个字典中,将三个⼈的名字⽤作键;#对于其中的每个⼈,都存储他喜欢的1~3个地⽅。为让这个练习更有趣些,可让⼀些朋友指出
#他们喜欢的⼏个地⽅。遍历这个字典,并将其中每个⼈的名字及其喜欢的地⽅打印出来。
favorite_places = {
"wps":{"成都","新疆","⼴州"},
"tq":{"成都","内江","⼤⾢"},
"lqz":{"重庆","遂宁","海南"}
}
for name ,citys in favorite_places.items():
print("Hi," + name.title() +",你喜欢的城市是")
for city in citys:
print(":" + city)
#6-10喜欢的数字:修改为完成练习6-2⽽编写的程序,让每个⼈都可以有多个喜欢的数
#字,然后将每个⼈的名字及其喜欢的数字打印出来。
favourite_number = {
"wwz":{"8","2","9"},
"fp":{"7","3","1"},
"lmh":{"6","8","9"},
"aaa":{"5","2","4"},
"bbb":{"4","6","0"}
}
for name , numbers in favourite_number.items():
print(name.title() + ",你喜欢的数字是:")
for number in numbers:
print(number)
#6-11 城市:创建⼀个名为cities的字典,其中将三个城市名⽤作键;对于每座城市,都
#创建⼀个字典,并在其中包含该城市所属的国家、⼈⼝约数以及⼀个有关该城市的事实。在
#表⽰每座城市的字典中,应包含country、population和fact等键。将每座城市的名字
#以及有关它们的信息都打印出来。
cities = {
"chengdu":{"country":"china","population":"20million","fact":"delicious"},
"london":{"country":"England","population":"30million","fact":"old"}, "Tokyo":{"country":"Japan","population":"40million","fact":"crowd"} }
for city , inof in cities.items():
print(city.title()+ " in " + inof["country"].title() +
".It has a population of " + inof["population"] + "," +
"it fact"s " + inof["fact"] + ".")
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论