我是靠谱客的博主 年轻钢笔,最近开发中收集的这篇文章主要介绍《Python编程:从入门到实践》第3章部分习题前言,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

文章目录

  • 前言
    • 3-4
    • 3-5
    • 3-6
    • 3-7


前言

Python初学者一枚,本文仅为个人学习记录,便于以后查看使用。

3-4

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

输入:

# Method 1
invite_list = ['Edward Elric', 'Alphonse Elric', 'Winry Rockbell', 'Roy Mustang']
print("Method 1:")
print(invite_list[0] + ", I would like to invite you for dinner.")
print(invite_list[1] + ", I would like to invite you for dinner.")
print(invite_list[2] + ", I would like to invite you for dinner.")
print(invite_list[-1] + ", I would like to invite you for dinner.")
print("n")
# Method 2
print("Method 2:")
for invite_name in invite_list:
print("Miss./Mr." + invite_name + ", I would like to invite you for dinner.")

输出:

Method 1:
Edward Elric, I would like to invite you for dinner.
Alphonse Elric, I would like to invite you for dinner.
Winry Rockbell, I would like to invite you for dinner.
Roy Mustang, I would like to invite you for dinner.
Method 2:
Miss./Mr.Edward Elric, I would like to invite you for dinner.
Miss./Mr.Alphonse Elric, I would like to invite you for dinner.
Miss./Mr.Winry Rockbell, I would like to invite you for dinner.
Miss./Mr.Roy Mustang, I would like to invite you for dinner.

3-5

修改嘉宾名单:你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。
1.以完成练习 3-4 时编写的程序为基础,在程序末尾添加一条 print 语句,指出哪位嘉宾无法赴约。
2.修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
3.再次打印一系列消息,向名单中的每位嘉宾发出邀请。

输入:

invite_list = ['Edward Elric', 'Alphonse Elric', 'Winry Rockbell', 'Roy Mustang']
print(invite_list[-1] + " is too busy to keep an appointment.n")
invite_list[-1] = 'Lzumi Curtis'
for invite_name in invite_list:
print("Mr./Miss." + invite_name + ", I would like to invite you for dinner.")

问题2利用第三行代码直接解决。

输出:

Roy Mustang is too busy to keep an appointment.
Mr./Miss.Edward Elric, I would like to invite you for dinner.
Mr./Miss.Alphonse Elric, I would like to invite you for dinner.
Mr./Miss.Winry Rockbell, I would like to invite you for dinner.
Mr./Miss.Lzumi Curtis, I would like to invite you for dinner.

3-6

添加嘉宾:你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。
1.以完成练习 3-4 或练习 3-5 时编写的程序为基础,在程序末尾添加一条 print 语句,指出你找到了一个更大的餐桌。
2.使用 insert()将一位新嘉宾添加到名单开头。
3.使用 insert()将另一位新嘉宾添加到名单中间。
4.使用 append()将最后一位新嘉宾添加到名单末尾。
5.打印一系列消息,向名单中的每位嘉宾发出邀请。

输入:

invite_list = ['Edward Elric', 'Alphonse Elric', 'Winry Rockbell', 'Roy Mustang']
print(invite_list[-1] + " is too busy to keep an appointment.n")
invite_list[-1] = 'Lzumi Curtis'
print("I found a bigger table.n")
invite_list.insert(0,'Alex Louis Armstrong')
print(invite_list)
invite_list.insert(3, 'Riza Hawkeye')
print(invite_list)
invite_list.append('Hohenheim Elric')
print(invite_list)
print("n")
for invite_name in invite_list:
print("Mr./Miss." + invite_name + ", I would like to invite you for dinner.")

列表名称.insert(索引,值):invite_list.insert(0,‘Alex Louis Armstrong’)
列表名称.append(值):invite_list.append(‘Hohenheim Elric’)

输出:

Roy Mustang is too busy to keep an appointment.
I found a bigger table.
['Alex Louis Armstrong', 'Edward Elric', 'Alphonse Elric', 'Winry Rockbell', 'Lzumi Curtis']
['Alex Louis Armstrong', 'Edward Elric', 'Alphonse Elric', 'Riza Hawkeye', 'Winry Rockbell', 'Lzumi Curtis']
['Alex Louis Armstrong', 'Edward Elric', 'Alphonse Elric', 'Riza Hawkeye', 'Winry Rockbell', 'Lzumi Curtis', 'Hohenheim Elric']
Mr./Miss.Alex Louis Armstrong, I would like to invite you for dinner.
Mr./Miss.Edward Elric, I would like to invite you for dinner.
Mr./Miss.Alphonse Elric, I would like to invite you for dinner.
Mr./Miss.Riza Hawkeye, I would like to invite you for dinner.
Mr./Miss.Winry Rockbell, I would like to invite you for dinner.
Mr./Miss.Lzumi Curtis, I would like to invite you for dinner.
Mr./Miss.Hohenheim Elric, I would like to invite you for dinner.

3-7

缩减名单:你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。
1.以完成练习 3-6 时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。
2.使用 pop()不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。
3.对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。
4.使用 del 将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。

输入:

invite_list = ['Edward Elric', 'Alphonse Elric', 'Winry Rockbell', 'Roy Mustang']
print(invite_list[-1] + " is too busy to keep an appointment.n")
invite_list[-1] = 'Lzumi Curtis'
print("I found a bigger table.n")
invite_list.insert(0,'Alex Louis Armstrong')
invite_list.insert(3, 'Riza Hawkeye')
invite_list.append('Hohenheim Elric')
print(invite_list)
print("n")
print("I 'm sorry, the new dining table can not be delivered in time so we can only invite two guest.n")
n = int(len(invite_list))
while n > 2:
last_name = invite_list.pop()
print("Mr./Miss." + last_name + ", I'm sorry that I couldn't invite you to dinner.")
n = n-1
print("n")
for invite_name in invite_list:
print("Mr./Miss." + invite_name + ", I cordially invite you to dinner.")
print("n")
print(invite_list)
print("n")
invite_list.remove('Alex Louis Armstrong')
invite_list.remove('Edward Elric')
print(invite_list)

len(列表名称):计算列表的元素个数。具体用法见len()函数
while语句后要加冒号。
列表名称.pop():剔除该列表末尾元素。
列表名称.pop(索引):剔除该列表对应位置元素。

输出:

Roy Mustang is too busy to keep an appointment.
I found a bigger table.
['Alex Louis Armstrong', 'Edward Elric', 'Alphonse Elric', 'Riza Hawkeye', 'Winry Rockbell', 'Lzumi Curtis', 'Hohenheim Elric']
I 'm sorry, the new dining table can not be delivered in time so we can only invite two guest.
Mr./Miss.Hohenheim Elric, I'm sorry that I couldn't invite you to dinner.
Mr./Miss.Lzumi Curtis, I'm sorry that I couldn't invite you to dinner.
Mr./Miss.Winry Rockbell, I'm sorry that I couldn't invite you to dinner.
Mr./Miss.Riza Hawkeye, I'm sorry that I couldn't invite you to dinner.
Mr./Miss.Alphonse Elric, I'm sorry that I couldn't invite you to dinner.
Mr./Miss.Alex Louis Armstrong, I cordially invite you to dinner.
Mr./Miss.Edward Elric, I cordially invite you to dinner.
['Alex Louis Armstrong', 'Edward Elric']
[]

最后

以上就是年轻钢笔为你收集整理的《Python编程:从入门到实践》第3章部分习题前言的全部内容,希望文章能够帮你解决《Python编程:从入门到实践》第3章部分习题前言所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部