python学习第十篇——while 的灵活运用

 1 sandwiches_orders = ['apple','banana','mango',"apple","watermelon"]
 2 finished_sandwiches = []
 3 while "apple" in sandwiches_orders:
 4         sandwiches_orders.remove("apple")
 5 print("sorry ,The apple is sold out!/nyour ordered are:")
 6 print(sandwiches_orders)
 7 while sandwiches_orders:
 8     print("please wait for a moment,i'm making "+sandwiches_orders[0]+" for you.")
 9     finished_sandwiches.append(sandwiches_orders[0])
10     del sandwiches_orders[0]
11 print("your ordered are finished:")
12 for sandwiche in finished_sandwiches:
13     print("	"+str(sandwiche))

上述代码的功能是:检查sandwiches_orders列表中是否有"apple",如果有,则删除apple,打印剩余的水果,并将其逐个添加到finished_sandwiches中,并将其在sandwiches_orders中删除,需要注意的是,这里先打印谁,就先添加谁,更加符合实际点餐先来先到的情况。所以没有采用.pop()类

stay foolish,stay hungry
原文地址:https://www.cnblogs.com/shaonianpi/p/9474906.html