笔记出处(学习UP主视频记录) https://www.bilibili.com/video/av35698354?p=10
5.4.1 检查特殊元素
requested_toppings = ['mushrooms', 'green peppers', 'extra chees'] for requested_topping in requested_toppings: print ("Adding " + requested_topping + ".") print (" Finished making your pizza!")
Adding mushrooms.
Adding green peppers.
Adding extra chees.
Finished making your pizza!
requested_toppings = ['mushrooms', 'green peppers', 'extra chees'] #假设青椒没了,用完了 for requested_toppong in requested_toppings: if requested_toppong == 'green peppers': print ("Sorry, we are out of green penppers right now.") else: print ("Adding " + requested_toppong + ".") print (" Finished making your pizza!")
Adding mushrooms.
Sorry, we are out of green penppers right now.
Adding extra chees.
Finished making your pizza!
5.4.2 确定列表不是空的
requested_toppings = [] if requested_toppings: for requested_topping in requested_toppings: print ("Adding " + requested_topping + ".") print (" Finished making your pizza!") else: print ("Are you sure you want a plain pizza?")
Are you sure you want a plain pizza?
5.4.3 使用多个列表
available_toppongs = ['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_toppongs: print ("Adding " + requested_topping + ".") else: print ("Sorry, we don't have " + requested_topping + ".") print (" Finished making your pizza!")
Adding mushrooms.
Sorry, we don't have french fries.
Adding extra cheese.
Finished making your pizza!
Caesar卢尚宇
2020年3月13日