程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

0 basic learning python (15)

編輯:Python

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved