因为sandwich_orders.remove('pastrami'),删除的是第一个pastrami,sandwich_orders = ['fish','pastrami','beaf','pastrami']
otherwich_orders = sandwich_orders.pop(),弹出最后一个元素,即otherwich_orders = 'pastrami' ,sandwich_orders = ['fish','pastrami','beaf'],finish_orders.append(otherwich_orders),那么此时finish_orders = ['pastrami']
之后循环,sandwich_orders.remove('pastrami'),删除'pastrami',此时sandwich_orders = ['fish','beaf']
之后otherwich_orders = sandwich_orders.pop(),弹出最后一个元素,即otherwich_orders ='beaf',finish_orders.append(otherwich_orders),那么此时finish_orders = ['pastrami','beaf']
循环结束。
代码可改为如下:
- sandwich_orders = ['pastrami','fish','pastrami','beaf','pastrami']
- finish_orders = []
- print('The pastrami is sale out')
- while 'pastrami' in sandwich_orders :
- sandwich_orders.remove('pastrami')
- for finish_order in sandwich_orders:
- print('Finish the ' + str(finish_order) + ' sandwich')
复制代码