ValueError: list.remove(x): x not in list报错

要求:遍历列表sandwich_order,删除其中所有'fish sandwich',重新打印列表sandwich_order

sandwich_order=['fish sandwich','beef sandwich','meat sandwich',
                'fish sandwich','fish sandwich']
while sandwich_order:
    sandwich_order.remove('fish sandwich')
print(sandwich_order)

为什么老是显示ValueError: list.remove(x): x not in list报错?谢谢。

因为你没有准确的写什么时候退出循环,然后它就一直在那删,当列表里没有这个元素的时候你还去删,它就会报错

while 循环没有设置终止条件 , 会一直执行
当 'fish sandwich' 不存在列表中的时候就会报错