鱼C论坛

 找回密码
 立即注册
查看: 626|回复: 1

[已解决]求问这道题如何改进?

[复制链接]
发表于 2019-3-13 22:22:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
五香烟熏牛肉( 五香烟熏牛肉(pastrami)卖完了 )卖完了 :使用为完成练习7-8而创建的列表sandwich_orders ,并确保'pastrami' 在其中至少出现了三次。在程序开头附近添加 这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while 循环将列表sandwich_orders 中的'pastrami' 都删除。确认最终的列 表finished_sandwiches 中不包含'pastrami' 。

sandwich_orders = ['pastrami','fish','pastrami','beaf','pastrami']
finish_orders = []
print('The pastrami is sale out')
while 'pastrami' in sandwich_orders :
    sandwich_orders.remove('pastrami')
    otherwich_orders = sandwich_orders.pop()
    finish_orders.append(otherwich_orders)
for finish_order in finish_orders :
    print('Finish the ' + str(finish_order) + ' sandwich')
================
打印出来结果还是有'pastrami'这个单词在里面,而想要的‘fish’不见了。。
A)}RYEG%I12$ZV]L@XBRT5S.png
最佳答案
2019-3-13 22:45:22
因为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']
循环结束。

代码可改为如下:
  1. sandwich_orders = ['pastrami','fish','pastrami','beaf','pastrami']
  2. finish_orders = []

  3. print('The pastrami is sale out')
  4. while 'pastrami' in sandwich_orders :
  5.     sandwich_orders.remove('pastrami')
  6. for finish_order in sandwich_orders:
  7.     print('Finish the ' + str(finish_order) + ' sandwich')
复制代码



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-13 22:45:22 | 显示全部楼层    本楼为最佳答案   
因为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']
循环结束。

代码可改为如下:
  1. sandwich_orders = ['pastrami','fish','pastrami','beaf','pastrami']
  2. finish_orders = []

  3. print('The pastrami is sale out')
  4. while 'pastrami' in sandwich_orders :
  5.     sandwich_orders.remove('pastrami')
  6. for finish_order in sandwich_orders:
  7.     print('Finish the ' + str(finish_order) + ' sandwich')
复制代码



小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-4 08:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表