了解IT技术
老九你最好的选择

Python编程:从入门到实践—-课后习题 3-1 ~ 3-11 答案

练习3-1  姓名

将一些朋友的姓名存储在一个列表中,并将其命名为names。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。

names = ['ron', 'tyler', 'dani']

print(names[0])
print(names[1])
print(names[2])

输出:

ron
tyler
dani
练习3-2  问候语

继续使用练习3-1中的列表,但不打印每个朋友的姓名,而是打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。

names = ['ron', 'tyler', 'dani']

msg = "Hello,{0} ".format(names[0].title())
print(msg)

msg = "Hello,{0} ".format(names[1].title())
print(msg)

msg = "Hello, {0} ".format(names[2].title())
print(msg)

输出:

Hello, Ron!
Hello, Tyler!
Hello, Dani!
练习3-3 自己的列表

想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如“I would like to own a Honda motorcycle”。

car = ['Honda motorcycle','Honda motor','TOYOTA motor']

cars = 'I would like to own a {0}'.format(car[0].title())
print(cars)

cars = 'I would like to own a {0}'.format(car[1].title())
print(cars)

cars = 'I would like to own a {0}'.format(car[2].title())
print(cars)

输出:

I would like to own a Honda Motorcycle
I would like to own a Honda Motor
I would like to own a Toyota Motor
练习3-4  嘉宾名单

如果你可以邀请任何人一起共进晚餐(无论是故去的还是在世的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。

guests = ['guido van rossum', 'jack turner', 'lynn hill']

name = guests[0].title()
print(name + ", please come to dinner.")

name = guests[1].title()
print(name + ", please come to dinner.")

name = guests[2].title()
print(name + ", please come to dinner.")

输出:

Guido Van Rossum, please come to dinner.
Jack Turner, please come to dinner.
Lynn Hill, please come to dinner.
练习3-5  修改嘉宾名单

你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。

  • 以你完成练习3-4时编写的程序为基础,在程序末尾添加一条print语句,指出哪位嘉宾无法赴约。
  • 修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
  • 再次打印一系列消息,将名单中的每位嘉宾发出邀请。
# 邀请一些嘉宾与你共进晚餐。
guests = ['guido van rossum', 'jack turner', 'lynn hill']

name = guests[0].title()
print(name + ", please come to dinner.")

name = guests[1].title()
print(name + ", please come to dinner.")

name = guests[2].title()
print(name + ", please come to dinner.")

name = guests[1].title()
print("\nSorry, " + name + " can't make it to dinner.")

# Jack无法赴约,转而邀请Gary。
del(guests[1])
guests.insert(1, 'gary snyder')

# 重新打印邀请函。
name = guests[0].title()
print("\n" + name + ", please come to dinner.")

name = guests[1].title()
print(name + ", please come to dinner.")

name = guests[2].title()
print(name + ", please come to dinner.")

输出:

Guido Van Rossum, please come to dinner.
Jack Turner, please come to dinner.
Lynn Hill, please come to dinner.

Sorry, Jack Turner can't make it to dinner.

Guido Van Rossum, please come to dinner.
Gary Snyder, please come to dinner.
Lynn Hill, please come to dinner.
练习3-6  添加嘉宾

你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。

以你完成练习3-4或3-5时编写的程序为基础,在程序末尾添加一条print语句,指出你找到了一个更大的餐桌。

  • 使用insert()将一位新嘉宾添加到名单开头。
  • 使用insert()将另一位新嘉宾添加到名单中间。
  • 使用append()将最后一位新嘉宾添加到名单末尾。
  • 打印一系列消息,向名单中的每位嘉宾发出邀请。
# 邀请一些嘉宾与你共进晚餐。
guests = ['guido van rossum', 'jack turner', 'lynn hill']

name = guests[0].title()
print(name + ", please come to dinner.")

name = guests[1].title()
print(name + ", please come to dinner.")

name = guests[2].title()
print(name + ", please come to dinner.")

name = guests[1].title()
print("\nSorry, " + name + " can't make it to dinner.")

# Jack无法赴约,转而邀请Gary。
del(guests[1])
guests.insert(1, 'gary snyder')

# 重新打印邀请函。
name = guests[0].title()
print("\n" + name + ", please come to dinner.")

name = guests[1].title()
print(name + ", please come to dinner.")

name = guests[2].title()
print(name + ", please come to dinner.")

# 找到了更大的餐桌,再邀请一些嘉宾。
print("\nWe got a bigger table!")
guests.insert(0, 'frida kahlo')
guests.insert(2, 'reinhold messner')
guests.append('elizabeth peratrovich')

name = guests[0].title()
print(name + ", please come to dinner.")

name = guests[1].title()
print(name + ", please come to dinner.")

name = guests[2].title()
print(name + ", please come to dinner.")

name = guests[3].title()
print(name + ", please come to dinner.")

name = guests[4].title()
print(name + ", please come to dinner.")

name = guests[5].title()
print(name + ", please come to dinner.")

输出:

Guido Van Rossum, please come to dinner.
Jack Turner, please come to dinner.
Lynn Hill, please come to dinner.

Sorry, Jack Turner can't make it to dinner.

Guido Van Rossum, please come to dinner.
Gary Snyder, please come to dinner.
Lynn Hill, please come to dinner.

We got a bigger table!
Frida Kahlo, please come to dinner.
Guido Van Rossum, please come to dinner.
Reinhold Messner, please come to dinner.
Gary Snyder, please come to dinner.
Lynn Hill, please come to dinner.
Elizabeth Peratrovich, please come to dinner.
练习3-7  缩减名单

你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。

  • 以你完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。
  • 使用pop()不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。
  • 对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在邀请之列。
  • 使用del将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。
# 邀请一些嘉宾与你共进晚餐。
guests = ['guido van rossum', 'jack turner', 'lynn hill']

name = guests[0].title()
print(name + ", please come to dinner.")

name = guests[1].title()
print(name + ", please come to dinner.")

name = guests[2].title()
print(name + ", please come to dinner.")

name = guests[1].title()
print("\nSorry, " + name + " can't make it to dinner.")

# Jack无法赴约,转而邀请Gary。
del(guests[1])
guests.insert(1, 'gary snyder')

# 重新打印邀请函。
name = guests[0].title()
print("\n" + name + ", please come to dinner.")

name = guests[1].title()
print(name + ", please come to dinner.")

name = guests[2].title()
print(name + ", please come to dinner.")

# 找到了更大的餐桌,再邀请一些嘉宾。
print("\nWe got a bigger table!")
guests.insert(0, 'frida kahlo')
guests.insert(2, 'reinhold messner')
guests.append('elizabeth peratrovich')

name = guests[0].title()
print(name + ", please come to dinner.")

name = guests[1].title()
print(name + ", please come to dinner.")

name = guests[2].title()
print(name + ", please come to dinner.")

name = guests[3].title()
print(name + ", please come to dinner.")

name = guests[4].title()
print(name + ", please come to dinner.")

name = guests[5].title()
print(name + ", please come to dinner.")

# 糟糕,餐桌无法及时送达!
print("\nSorry, we can only invite two people to dinner.")

name = guests.pop()
print("Sorry, " + name.title() + " there's no room at the table.")

name = guests.pop()
print("Sorry, " + name.title() + " there's no room at the table.")

name = guests.pop()
print("Sorry, " + name.title() + " there's no room at the table.")

name = guests.pop()
print("Sorry, " + name.title() + " there's no room at the table.")

# 应该只剩下两位嘉宾了,向他们发出邀请。
name = guests[0].title()
print(name + ", please come to dinner.")

name = guests[1].title()
print(name + ", please come to dinner.")

# 清空名单。
del(guests[0])
del(guests[0])

# 核实名单是空的。
print(guests)

输出:

Guido Van Rossum, please come to dinner.
Jack Turner, please come to dinner.
Lynn Hill, please come to dinner.

Sorry, Jack Turner can't make it to dinner.

Guido Van Rossum, please come to dinner.
Gary Snyder, please come to dinner.
Lynn Hill, please come to dinner.

We got a bigger table!
Frida Kahlo, please come to dinner.
Guido Van Rossum, please come to dinner.
Reinhold Messner, please come to dinner.
Gary Snyder, please come to dinner.
Lynn Hill, please come to dinner.
Elizabeth Peratrovich, please come to dinner.

Sorry, we can only invite two people to dinner.
Sorry, Elizabeth Peratrovich there's no room at the table.
Sorry, Lynn Hill there's no room at the table.
Sorry, Gary Snyder there's no room at the table.
Sorry, Reinhold Messner there's no room at the table.
Frida Kahlo, please come to dinner.
Guido Van Rossum, please come to dinner.
[]
练习3-8. 放眼世界

想出至少5个你渴望去旅游的地方。

  • 将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。
  • 按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python列表。
  • 使用sorted()按字母顺序打印这个列表,同时不修改它。
  • 再次打印该列表,核实排列顺序未变。
  • 使用sorted()按与字母顺序相反的顺序打印这个列表,同时不修改它。
  • 再次打印该列表,核实排列顺序未变。
  • 使用reverse()修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。
  • 使用reverse()再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。
  • 使用sort()修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了。
  • 使用sort()修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。
locations = ['himalaya', 'andes', 'tierra del fuego', 'labrador', 'guam']

print("Original order:")
print(locations)

print("\nAlphabetical:")
print(sorted(locations))

print("\nOriginal order:")
print(locations)

print("\nReverse alphabetical:")
print(sorted(locations, reverse=True))

print("\nOriginal order:")
print(locations)

print("\nReversed:")
locations.reverse()
print(locations)

print("\nOriginal order:")
locations.reverse()
print(locations)

print("\nAlphabetical")
locations.sort()
print(locations)

print("\nReverse alphabetical")
locations.sort(reverse=True)
print(locations)

输出:

Original order:
['himalaya', 'andes', 'tierra del fuego', 'labrador', 'guam']

Alphabetical:
['andes', 'guam', 'himalaya', 'labrador', 'tierra del fuego']

Original order:
['himalaya', 'andes', 'tierra del fuego', 'labrador', 'guam']

Reverse alphabetical:
['tierra del fuego', 'labrador', 'himalaya', 'guam', 'andes']

Original order:
['himalaya', 'andes', 'tierra del fuego', 'labrador', 'guam']

Reversed:
['guam', 'labrador', 'tierra del fuego', 'andes', 'himalaya']

Original order:
['himalaya', 'andes', 'tierra del fuego', 'labrador', 'guam']

Alphabetical
['andes', 'guam', 'himalaya', 'labrador', 'tierra del fuego']

Reverse alphabetical
['tierra del fuego', 'labrador', 'himalaya', 'guam', 'andes']
练习3-9. 晚餐嘉宾

在完成练习3-4~练习3-7时编写的程序之一中,使用len() 打印一条消息,指出你邀请了多少位嘉宾来与你共进晚餐。

guests = ['guido van rossum', 'jack turner', 'lynn hill']

name = guests[0].title()
print(name + ", please come to dinner.")

name = guests[1].title()
print(name + ", please come to dinner.")

name = guests[2].title()
print(name + ", please come to dinner.")

print('I invite {0} guests to dinner.'.format(str(len(guests))))

输出:

Guido Van Rossum, please come to dinner.
Jack Turner, please come to dinner.
Lynn Hill, please come to dinner.
I invite 3 guests to dinner.
练习3-10. 尝试使用各个函数

想想可存储到列表中的东西,如山岳、河流、国家、城市、语言或你喜欢的任何东西。编写一个程序,在其中创建一个包含这些元素的列
表,然后,对于本章介绍的每个函数,都至少使用一次来处理这个列表。

cities = ['Peking', 'Shanghai', 'Guangzhou', 'Hongkong']
print(cities)
cities[0] = 'Beijing'
print(cities)
cities.append('Shenzhen')
print(cities)
cities.insert(3, 'Wuhan')
print(cities)
del cities[1]
print(cities)
cities.pop()
print(cities)
cities.remove('Guangzhou')
print(cities)
print(sorted(cities))
cities.reverse()
print(cities)
cities.sort()
print(cities)
cities.sort(reverse = True)
print(cities)

输出:

['Peking', 'Shanghai', 'Guangzhou', 'Hongkong']
['Beijing', 'Shanghai', 'Guangzhou', 'Hongkong']
['Beijing', 'Shanghai', 'Guangzhou', 'Hongkong', 'Shenzhen']
['Beijing', 'Shanghai', 'Guangzhou', 'Wuhan', 'Hongkong', 'Shenzhen']
['Beijing', 'Guangzhou', 'Wuhan', 'Hongkong', 'Shenzhen']
['Beijing', 'Guangzhou', 'Wuhan', 'Hongkong']
['Beijing', 'Wuhan', 'Hongkong']
['Beijing', 'Hongkong', 'Wuhan']
['Hongkong', 'Wuhan', 'Beijing']
['Beijing', 'Hongkong', 'Wuhan']
['Wuhan', 'Hongkong', 'Beijing']
练习3-11. 有意引发错误

如果你还没有在程序中遇到过索引错误,就尝试引发一个这种错误。在你的一个程序中,修改其中的索引,以引发索引错误。关闭程序前,务必消除这个错误。

赞(3) 打赏
未经允许不得转载:老九IT技术网 » Python编程:从入门到实践—-课后习题 3-1 ~ 3-11 答案

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

老九为IT技术人提供最全面的IT资讯和交流互动

欢迎投稿广告合作

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏