书籍中的示例问题
如果我想查看pastrami
的列表,并确保finished
中没有熏牛肉,这很好:
orders = ['tuna sub', 'chicken parmasean', 'pastrami', 'chicken teryiaki', 'pastrami']
finished = []
for order in orders:
while 'pastrami' in orders:
orders.remove('pastrami')
print("preparing " + order + " ...")
finished.append(order)
for sandwich in finished:
print(sandwich + " is ready")
└─$ python3 sandwiches.py
preparing tuna sub ...
preparing chicken parmasean ...
preparing chicken teryiaki ...
tuna sub is ready
chicken parmasean is ready
chicken teryiaki is ready
但是使用CCD_ 3来检查CCD_。
orders = ['tuna sub', 'chicken parmasean', 'pastrami', 'chicken teryiaki', 'pastrami']
finished = []
for order in orders:
if order == 'pastrami':
orders.remove('pastrami')
print("preparing " + order + " ...")
finished.append(order)
for sandwich in finished:
print(sandwich + " is ready")
─$ python3 sandwiches.py
preparing tuna sub ...
preparing chicken parmasean ...
preparing pastrami ...
preparing pastrami ...
tuna sub is ready
chicken parmasean is ready
pastrami is ready
pastrami is ready
我不明白为什么会发生这种事?
也许你想否定你的if
?
finished = []
for order in orders:
if order != 'pastrami':
print("preparing " + order + " ...")
finished.append(order)