5.4.3 使用多個列表

Ohm發表於2018-09-03
available_toppings = ['mushrooms','olives','green peppers','pepperoni','pineapple','extra cheese']

requested_toppings = ['mushrooms','french fries','extra cheese']

for requested_topping in requested_toppings: 
    if requested_topping in available_toppings:
        print('Adding '+ requested_topping + '.')
    else:
        print("Sorry, we don't have " + requested_topping +".")
print('\nfinished making your pizza!')

在列表裡,我們遍歷顧客點的配料列表。在for語句迴圈中,對顧客點的每個配料,都會檢查它是否在供應的配料表裡。如果在供應列表裡有,則把它加入到披薩中,否則將執行else程式碼塊

相關文章