pepperoni使用完了,就告诉用户,已经用完了 if requested_topping == 'pepperoni': print("Sorry, we are out of pepperoni right now.") else: print("Adding " + requested_topping + '.')
print("\nFinished making your pizza.") 这里在比萨中添加每种配料前都进行检查。代码检查顾客点的是否是青椒,如果是,就显示一条信息,指出不能点青椒的原因。else处代码块确保其他配料 都将添加到比萨中。 输出表明,妥善地处理了顾客点的每种配料: Adding mushrooms. Adding extra cheese. Sorry, we are out of pepperoni right now.
Finished making your pizza.
5.4.2 确定列表不是空的 到目前为止,对于处理的每个列表都做了一个简单的假设,即假设它们都至少包含一个元素。我们马上就要让用户来提供存储在列表中的信息,因此不能再 假设循环运行时列表不是空的。有鉴于此,在运行for循环前确定列表是否为空很重要。 下面在制作比萨前检查顾客点的配料列表是否为空。如果列表是空的,就向顾客确认她是否要点普通披萨;如果列表不是空的,就像前面示例那样制作比萨:
requested_toppings = [] #判断列表是否是空的,如果是空的没有判断,就会出现错误,所以要事先判断列表是不是空列表 if requested_toppings: for requested_topping in requested_toppings: if requested_topping == 'pepperoni': print("Sorry, we are out of pepperoni right now.") else: print("Adding " + requested_topping + '.') print("\nFinished making your pizza.") else: print("Are you sure you want a plain pizza?") 在这里,我们首先创建了一个空列表,其中不包含任何配料。第一个if语句进行简单检查,而不是直接执行for循环。在if语句中将列表名用在条件表达式 中时,Python将在列表中至少包含一个元素返回True,并在列表为空是返回False。如果requested_toppings不为空,就运行与前一个示例相同的for循环; 否则,就打印一条消息,询问顾客是否确实要点不加任何配料的普通比萨。 在这里,这个列表为空,因此输出如下——询问顾客是否确实要点普通披萨: Are you sure you want a plain pizza? 如果这个列表不为空,将显示在比萨中添加的各种配料的输出。
5.4.3 使用多个列表 顾客的要求往往五花八门,在比萨配料方面尤其如此。如果顾客要在比萨中添加炸薯条,该怎么办呢?可使用列表和if语句来确定能否满足顾客的要求。 来看看在制作比萨前如何拒绝怪异的配料要求。下面的示例定义了两个列表,其中第一个列表包含比萨店供应的配料,而第二个列表包含顾客点的配料。 这次对于requested_toppings中的每个元素,都检查它是否是比萨店供应的配料,在决定是否在比萨中添加它:
available_toppings = ['mushrooms','olives','green peppers','pepperoni','pineapple','extra cheese'] requested_toppings = ['mushrooms','french fries','extra cheese']
for requested_topping in requested_toppings: if requested_topping in available_toppings: print("Adding " + requested_topping + '.') else: print("Sorry, we don't have " + requested_topping + '.') print("\nFinished making your pizza.") 首先我们定义了一个比萨店能够提供配料的列表。请注意,如果比萨店供应的配料是固定的,也可以使用一个元组来存储它们。接着,我们有定义了一个 列表,其中包含顾客点的配料,请注意那个不同寻常的配料——'french fries'.接着我们遍历顾客点的配料列表。在这个循环中,对于顾客点的每种配料, 我们都检查它是否包含在供应的配料里诶保重;如果答案是肯定的,就将其加入到比萨中,否则将运行False代码块:打印一条消息,告诉顾客不供应这种配料 这些代码的输出整洁而详实: Adding mushrooms. Sorry, we don't have french fries. Adding extra cheese.
Finished making your pizza.
动手试一试 5-8 以特殊方式跟管理员打招呼:本题考察的是for循环和if语句的使用,在用户登录网站的时候,会根据用户的性质进行判断
login_users = ['admin','zhangxx','loujq','xiaoaj','liuxs']
for login_user in login_users: if login_user == 'admin': print("Hello admin,would you like to see a status report?") else: print("Hello Eric,thank you for logging in again.") 5-9 处理没有用户的情形:本题首先判断用户列表是否是空,如果是空,执行什么操作,如果不是空,则执行另外的操作:
login_users = [] #判断列表是否为空,如果为空,则执行空对应的语句 if login_users: for login_user in login_users: if login_user == 'admin': print("Hello admin,would you like to see a status report?") else: print("Hello Eric,thank you for logging in again.") else: print("We need to find some users!") 5-10 检查用户名
current_users = ['admin','zqq','lXs','ljq','dls','gw','sC'] new_users = ['gcx','zmz','lxs','gjz','sc'] transfor_current_users = [] #进行列表转换,把用户名都转化为小写,以确保用户名的唯一性 for current_user in current_users: transfor_current_users.append(current_user.title()) |