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