We're right if With a general understanding, we can apply it in practice , When we were in a pizza restaurant and the customer was ordering , We can use a simple for loop , List everything on the menu .
requested_toppings=['mushrooms','green peppers','extra cheese']
for requested—topping in requested_toppings:
print(f"adding {
requested_topping}.")
print("\nfinished making your pizza")
We used a very simple for loop , If our green pepper green peppers Use up , Then we should use a if Statement to explain .
requested_toppings=['mushrooms','green peppers','extra cheese']
for requested_topping in requested_toppings:
if requesed_topping=='green peppers':
print("sorry we are not have green peppers")
else
print(f"adding {
requested_topping}.")
print("\nfinished making your pizza")
We use a if The statement outputs... Without green pepper sorry we are not have green peppers. Similarly, we can input all the items in the list to check. If not, we can point out the reason .
So far, we have added at least one element to the list every time , But we're going to let users store things in the list , So we need to make sure that the list is empty .
The following example is to check whether the ingredients list before the customer orders is empty , If the list is empty, the customer will want to output whether they want plain pizza , If the list is not empty, we will add the customer's input to make it .
requested_toppings=[]
if requested_topping:
for requestede_toppings:
print(f"adding{
requested_topping}.")
print("\nfinish making your pizza")
else
print("are you sure you want a plain pizza")
We first create an empty list , There are no ingredients , Then check , When you include an element in the list, it will return ture Then output the completed statement , When the list is still empty, it will return false, Output at this time else The statement in , If this list is not empty, all the ingredients will be added to the pizza .