《Python编程:从入门到实践》第六章6.3节课后作业

本文代码是在jupyter中实现的,仅为了自我督促学习python之用。

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

代码:

python_words = {'List':'由一系列按特定顺序排列的元素组成','range()': '生成一系列的数字。', 'Tuple':'不可变得列表被称为元组。', 'str()': '将非字符串值表示成字符串。', '#': '井号后面的内容被称为注释。'}
for key,value in python_words.items():
    print("\nKey:" + key)
    print("Value:" + value)

运行结果:


Key:List
Value:由一系列按特定顺序排列的元素组成

Key:range()
Value:生成一系列的数字。

Key:Tuple
Value:不可变得列表被称为元组。

Key:str()
Value:将非字符串值表示成字符串。

Key:#
Value:井号后面的内容被称为注释。

6-5 河流:创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键 —值对可能是 ‘nile’: ‘egypt’ 。
 使用循环为每条河流打印一条消息,如“The Nile runs through Egypt.”。
 使用循环将该字典中每条河流的名字都打印出来。
 使用循环将该字典包含的每个国家的名字都打印出来。

代码:

Message = {'Nile':'Egypt','Amazon':'Brazil','Yangtze River':'China'}
for river,country in Message.items():
    print("The " + river.title() + " runs through " + country.title() + "." )

print("\n")
print("The three major rivers in the world are:")
for river in Message.keys():
    print(river.title())

print("\n")
print("The three major rivers in the world flow through the following countries:")
for country in Message.values():
    print(country.title())

运行结果:

The Nile runs through Egypt.
The Amazon runs through Brazil.
The Yangtze River runs through China.


The three major rivers in the world are:
Nile
Amazon
Yangtze River


The three major rivers in the world flow through the following countries:
Egypt
Brazil
China

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

First Method:
代码:

favorite_languages = {'jen':'python',
                     'sarah':'c',
                     'edward':'ruby',
                     'phil':'python'}
friends = ['jen', 'jack', 'sarah', 'edward', 'phil']

for friend in friends:
    if friend in favorite_languages.keys():
        print(name.title() + "," + "thank you for participating in this survey " + "!")
    else:
        print(friend.title() + "," + "sincerely invite you to participate in this survey activity" + ".")

运行结果:

Phil,thank you for participating in this survey !
Jack,sincerely invite you to participate in this survey activity.
Phil,thank you for participating in this survey !
Phil,thank you for participating in this survey !
Phil,thank you for participating in this survey !

Second Method:
代码:

favorite_languages = {'jen':'python',
                     'sarah':'c',
                     'edward':'ruby',
                     'phil':'python'}
friends = ['jen', 'jack', 'sarah', 'edward', 'phil']

for name in favorite_languages.keys():
    if name in friends:
        print(name.title() + "," + "thank you for participating in this survey " + "!")

for friend in friends:
    if friend not in favorite_languages.keys():
        print(friend.title() + "," + "sincerely invite you to participate in this survey activity" + ".")

运行结果:

Jen,thank you for participating in this survey !
Sarah,thank you for participating in this survey !
Edward,thank you for participating in this survey !
Phil,thank you for participating in this survey !
Jack,sincerely invite you to participate in this survey activity.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值