[python作业] [第三周] [周三]

6-3 词汇表: Python 字典可用于模拟现实生活中的字典,但为避免混淆,我们将后
者称为词汇表。
 想出你在前面学过的 5 个编程词汇,将它们用作词汇表中的键,并将它们的含
义作为值存储在词汇表中。
 以整洁的方式打印每个词汇及其含义。为此,你可以先打印词汇,在它后面加
上一个冒号,再打印词汇的含义;也可在一行打印词汇,再使用换行符( \n)

6-4 词汇表 2:既然你知道了如何遍历字典,现在请整理你为完成练习 6-3 而编写
的代码,将其中的一系列 print 语句替换为一个遍历字典中的键和值的循环。确定该循环正确无误后,再在词汇表中添加 5 个 Python 术语。当你再次运行这个程序时,这些
新术语及其含义将自动包含在输出中。

vocabulary = {
    'name': 'A name is a term used for identification',
    'age': 'The effect of time on a person',
    'birthday': 'A birthday is the anniversary of the birth of a person or an institution. '
}

for word, meaning in vocabulary.items():
    print(word + ': ' + meaning)
Output:
vocabulary = {
    'name': 'A name is a term used for identification',
    'age': 'The effect of time on a person',
    'birthday': 'A birthday is the anniversary of the birth of a person or an institution. '
}

for word, meaning in vocabulary.items():
    print(word + ': ' + meaning)

vocabulary['new'] = 'New is an adjective referring to something recently made, discovered, or created.'
vocabulary['music'] = 'Music is an art form and cultural activity whose medium is sound organized in time.'
vocabulary['movie'] = "A film, also called a movie, motion picture, theatrical film, or photoplay, is a series of still images that, when shown on a screen, create the illusion of moving images."
vocabulary['apple'] = 'An apple is a sweet, edible fruit produced by an apple tree (Malus pumila). '
vocabulary['banbana'] = "A banana is an edible fruit – botanically a berry[1][2] – produced by several kinds of large herbaceous flowering plants in the genus Musa"

print("\nNow I added another 5 words:")
for word, meaning in vocabulary.items():
    print(word + ': ' + meaning)

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

name_list = ['nameA', 'nameB', 'nameC', 'nameD']
survey = {'nameA': 'Chiese', 'nameB': 'English'}
for name in name_list:
    if name in survey.keys():
        print(name, ", Thanks for your help!")
    else:
        print(name, ", Would you like to take part in our survey?")
Output:
nameA , Thanks for your help!
nameB , Thanks for your help!
nameC , Would you like to take part in our survey?
nameD , Would you like to take part in our survey?

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

dog = {'Type': 'dog', 'Owner': 'peopelA'}
cat = {'Type': 'cat', 'Owner': 'perpleB'}
turtle = {'Type': 'turtle', 'Owner': 'peopleC'}

pets = [dog, cat, turtle]
for pet in pets:
    print(pet)
Output:
{'Type': 'dog', 'Owner': 'peopelA'}
{'Type': 'cat', 'Owner': 'perpleB'}
{'Type': 'turtle', 'Owner': 'peopleC'}

6-11 城市:创建一个名为 cities 的字典,其中将三个城市名用作键;对于每座城
市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该城市
的事实。在表示每座城市的字典中,应包含 country、 population 和 fact 等键。将每座
城市的名字以及有关它们的信息都打印出来。

Beijing = {'country': 'China', 'population': '21.792 million', 'fact': 'The capital of China'}
Paris = {'country': 'France', 'population': '2 million', 'fact': 'Eiffel Tower is one of the symbols of Paris'}
London = {'country': 'England', 'population': '8.28 million', 'fact': 'One of the biggest financial center in the world'}

cities = {'Beijing': Beijing, 'Paris': Paris, 'London': London}

for city, infos in cities.items():
    print(city + ':')
    for key, value in infos.items():
        print(key + ': ' + value)
    print()
Output:
Beijing:
country: China
population: 21.792 million
fact: The capital of China

Paris:
country: France
population: 2 million
fact: Eiffel Tower is one of the symbols of Paris

London:
country: England
population: 8.28 million
fact: One of the biggest financial center in the world

猜你喜欢

转载自blog.csdn.net/ill__world/article/details/79645625