一、列表切片
1、选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。
打印消息“The first three items in the list are:”,再使用切片来打印列表的前三个 元素。
打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中 间的三个元素。
打印消息“The last three items in the list are:”,再使用切片来打印列表末尾的三 个元素。
players = ['charles', 'martina', 'michael', 'florence', 'eli',]
print("the first three items in the list are:")
for p1 in players[:3]:
print(p1)
print("Three items from the middle of the list are:")
lenth=int(len(players)/2-1)
print(lenth)
for p1 in players[lenth:lenth+3]:
print(p1)
print("The last three items in the list are:")
for p1 in players[-3:]:
print(p1)
二、元组
1、有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食 品,并将其存储在一个元组中。
使用一个 for 循环将该餐馆提供的五种食品都打印出来。
尝试修改其中的一个元素,核实 Python 确实会拒绝你这样做。
餐馆调整了菜单,替换了它提供的其中两种食品。请编写一个这样的代码块: 给元组变量赋值,并使用一个 for 循环将新元组的每个元素都打印出来。
T=("bread","friut","meet","humberger","chips")
for t in T:
print(t)
#t[2]="apple"出错,不允许修改元组元素的值
T=("bread","apple","banana","meet","chips")#可以修改元组变量的值
for t in T:
print(t)
三、if语句
1、假设在游戏中刚射杀了一个外星人,请创建一个名为 alien_color 的变量,并将其设置为'green'、'yellow'或'red'。
编写一条 if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出 玩家获得了 5 个点。
编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中 未通过
alien_color=input("请输入green,yello,或red:")
if alien_color=="green":
print("玩家获得5个点")
else:
print("未通过")
2、 if-elif-else 结构。
如果外星人是绿色的,就打印一条消息,指出玩家获得了 5 个点。
如果外星人是黄色的,就打印一条消息,指出玩家获得了 10 个点。
如果外星人是红色的,就打印一条消息,指出玩家获得了 15 个点。
编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条 消息。
alien_color=input(