高级编程技术 课后作业三

3-1 姓名:将一些朋友的姓名存储在一个列表中,并将其命名为names。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。
names = ['Micheal','James','Black','Smith']
for name in names:
    print(name)
Micheal
James
Black
Smith


3-2 问候语:继续使用练习3-1中的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。
names = ['Micheal','James','Black','Smith']
for name in names:
    print(name + ", nice to meet you!")
Micheal, nice to meet you!
James, nice to meet you!
Black, nice to meet you!
Smith, nice to meet you!

3-3 自己的列表:想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如“I would like to own a Honda motorcycle”。
vehicles = ['bycicle','motorcycle','car']
brands = ['GIANT','Honda','Benz']
for num in range(0,3):
    print("I would like to own a " + brands[num] + " " +vehicles[num] + ".")

I would like to own a GIANT bycicle.
I would like to own a Honda motorcycle.
I would like to own a Benz car.

3-4 嘉宾名单:如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?

3-5 修改嘉宾名单:你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。

3-5-1 已完成练习3-4时编写的程序为基础,在程序末尾添加一条print语句,指出哪位嘉宾无法赴约。

celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\n" + celebrities[2] + " can't have dinner together.")

Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?

K.J. Apa can't have dinner together.

3-5-2 修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。

celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\n" + celebrities[2] + " can't have dinner together.")

del celebrities[2]
celebrities.append("Rachel Antonoff")

3-5-2 再次打印一系列消息,向名单中的每位嘉宾发出邀请。

celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\n" + celebrities[2] + " can't have dinner together.\n")

del celebrities[2]
celebrities.append("Rachel Antonoff")

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?

K.J. Apa can't have dinner together.

Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
Rachel Antonoff, May I have the plea sure of inviting you to dinner?


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

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

celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")
    
print("\nI booked a bigger table.")

3-6-2 使用insert()将一位新嘉宾添加到名单开头。

celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")

print("\nI booked a bigger table.")

celebrities.insert(0,"Bar Refaeli")

3-6-3 使用insert()将另一位新嘉宾添加到名单中间。

celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")
    
print("\nI booked a bigger table.")

celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")

3-6-4 使用append()将最后一位新嘉宾添加到名单末尾。

celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")
    
print("\nI booked a bigger table.")

celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
celebrities.append("Daiane Conterato")

3-6-5 打印一系列消息,向名单中的每位嘉宾发出邀请。

celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

print("I booked a bigger table.\n")

celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
celebrities.append("Daiane Conterato")

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")
I booked a bigger table.

Bar Refaeli, May I have the plea sure of inviting you to dinner?
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
Cacee Cobb, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
Daiane Conterato, May I have the plea sure of inviting you to dinner?

3-7 缩减名单:你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。

3-7-1 已完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。

celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

print("I booked a bigger table.\n")

celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
celebrities.append("Daiane Conterato")

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")

print("\nI only can invite two guests because the table newly bought can't be delivered in time.")
I booked a bigger table.

Bar Refaeli, May I have the plea sure of inviting you to dinner?
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
Cacee Cobb, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
Daiane Conterato, May I have the plea sure of inviting you to dinner?

I only can invite two guests because the table newly bought can't be delivered in time.

3-7-2 使用pop()不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。

celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

print("I booked a bigger table.\n")

celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
celebrities.append("Daiane Conterato")

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")

print("\nI only can invite two guests because the table newly bought can't be delivered in time.\n")

while len(celebrities) > 2:
	name = celebrities.pop()
	print(name + ", I sorry that I can't invite you to have dinnner together.")
I booked a bigger table.

Bar Refaeli, May I have the plea sure of inviting you to dinner?
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
Cacee Cobb, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
Daiane Conterato, May I have the plea sure of inviting you to dinner?

I only can invite two guests because the table newly bought can't be delivered in time.

Daiane Conterato, I sorry that I can't invite you to have dinnner together.
Padma Lakshmi, I sorry that I can't invite you to have dinnner together.
K.J. Apa, I sorry that I can't invite you to have dinnner together.
Cacee Cobb, I sorry that I can't invite you to have dinnner together.
Ed Burns, I sorry that I can't invite you to have dinnner together.

3-7-3 对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。

celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

print("I booked a bigger table.\n")

celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
celebrities.append("Daiane Conterato")

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")

print("\nI only can invite two guests because the table newly bought can't be delivered in time.\n")

while len(celebrities) > 2:
	name = celebrities.pop()
	print(name + ", I sorry that I can't invite you to have dinnner together.")

for celebrity in celebrities:
	print(celebrity + ", I hope you can still have dinner with me.")
I booked a bigger table.

Bar Refaeli, May I have the plea sure of inviting you to dinner?
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
Cacee Cobb, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
Daiane Conterato, May I have the plea sure of inviting you to dinner?

I only can invite two guests because the table newly bought can't be delivered in time.

Daiane Conterato, I sorry that I can't invite you to have dinnner together.
Padma Lakshmi, I sorry that I can't invite you to have dinnner together.
K.J. Apa, I sorry that I can't invite you to have dinnner together.
Cacee Cobb, I sorry that I can't invite you to have dinnner together.
Ed Burns, I sorry that I can't invite you to have dinnner together.
Bar Refaeli, I hope you can still have dinner with me.
Aaron Eckhart, I hope you can still have dinner with me.

3-7-4 使用del将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。

celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

print("I booked a bigger table.\n")

celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
celebrities.append("Daiane Conterato")

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")

print("\nI only can invite two guests because the table newly bought can't be delivered in time.\n")

while len(celebrities) > 2:
	name = celebrities.pop()
	print(name + ", I sorry that I can't invite you to have dinnner together.")

for celebrity in celebrities:
	print(celebrity + ", I hope you can still have dinner with me.")
	
del celebrities[0]
del celebrities[0]
print("\nThe list is:")
print(celebrities)
I booked a bigger table.

Bar Refaeli, May I have the plea sure of inviting you to dinner?
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
Cacee Cobb, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
Daiane Conterato, May I have the plea sure of inviting you to dinner?

I only can invite two guests because the table newly bought can't be delivered in time.

Daiane Conterato, I sorry that I can't invite you to have dinnner together.
Padma Lakshmi, I sorry that I can't invite you to have dinnner together.
K.J. Apa, I sorry that I can't invite you to have dinnner together.
Cacee Cobb, I sorry that I can't invite you to have dinnner together.
Ed Burns, I sorry that I can't invite you to have dinnner together.
Bar Refaeli, I hope you can still have dinner with me.
Aaron Eckhart, I hope you can still have dinner with me.

The list is:
[]

3-8 放眼世界:想出至少5个你渴望去旅游的地方

3-8-1 将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。

Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]

3-8-2 按原始排列顺序打印该列表。

Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]

print(Resort)
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']

3-8-3 使用sorted()按字母顺序打印这个列表。

Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]

print(Resort)
print()
print(sorted(Resort))
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']

['Aegean Sea', 'Pink Sands', 'Rose Lake', 'Santorini', 'The blue hole']

3-8-4 再次打印该列表,核实排列顺序确实未变。

Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]

print(Resort)
print()
print(sorted(Resort))
print()
print(Resort)
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']

['Aegean Sea', 'Pink Sands', 'Rose Lake', 'Santorini', 'The blue hole']

['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']

3-8-5 使用sorted()按与字母顺序相反的顺序打印这个列表。

Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]

print(sorted(Resort,reverse = True))

['The blue hole', 'Santorini', 'Rose Lake', 'Pink Sands', 'Aegean Sea']

3-8-6 再次打印该列表,核实排列顺序确实未变。

Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]

print(sorted(Resort,reverse = True))
print()
print(Resort)
['The blue hole', 'Santorini', 'Rose Lake', 'Pink Sands', 'Aegean Sea']

['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']

3-8-7 使用reverse()修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。

Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]

print(Resort)

Resort.reverse()
print()
print(Resort)
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']

['The blue hole', 'Rose Lake', 'Pink Sands', 'Aegean Sea', 'Santorini']

3-8-8 使用reverse()再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。

Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]

print(Resort)

Resort.reverse()
print()
print(Resort)

Resort.reverse()
print()
print(Resort)
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']

['The blue hole', 'Rose Lake', 'Pink Sands', 'Aegean Sea', 'Santorini']

['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']

3-8-9 使用sort()修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了。

Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]

print(Resort)

Resort.sort()
print()
print(Resort)
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']

['Aegean Sea', 'Pink Sands', 'Rose Lake', 'Santorini', 'The blue hole']

3-8-10 使用sort()修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。

Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]

print(Resort)

Resort.sort(reverse = True)
print()
print(Resort)
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']

['The blue hole', 'Santorini', 'Rose Lake', 'Pink Sands', 'Aegean Sea']


3-9 晚餐嘉宾:在完成练习3-4~练习3-7时编写的程序之一中,使用len()打印一条消息,指出你邀请了多少位嘉宾来与你共进晚餐。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]

for celebrity in celebrities:
    print(celebrity + ", May I have the plea sure of inviting you to dinner?")
    
print("\nI total invite " + str(len(celebrities)) + " guests.")
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?

I total invite 4 guests.


3-10 尝试使用各个函数。编写一个程序,在其中创建一个包含这些元素的列表,然后,对于本章介绍的每个函数,都至少只用一次来处理这个列表。
words = ["cat","apple","dog","boy"]

words.insert(0,"egg")
word = words.pop()
words.append(word)
del words[len(words)-1]
words.remove(words[0])
words.sort()
words.reverse()
print(sorted(words))
['apple', 'cat', 'dog']


3-11 有意引发错误:在你的一个程序中,修改其中的索引,以引发索引错误。在关闭程序前,务必消除这个错误。
words = ["apple","boy"]

del words[0]
del words[1]

print(words)
Traceback (most recent call last):
  File "Test.py", line 4, in 
    del words[1]
IndexError: list assignment index out of range
修改为
words = ["apple","boy"]

del words[0]
del words[0]	# del words[1] is wrong.

print(words)
[]

你可能感兴趣的:(高级编程技术 课后作业三)