Jump out of the multilayer loop: three-layer loop, the innermost layer directly jumps out 3 layers
In Python, the function runs to return and stops, so you can use this feature to write functions as functions to terminate multiple loops
def work ():
For I in range (5):
Print ("i=", i)
For j in Range (5):
Print ("--j=", J)
for k in range (5):
If k<2:
Print ("------>k=", K)
Else
Return i,j,k
Print (Work ())
Shopping Cart Procedure
Requirements: 1. After starting the program, let the user enter the payroll and then print the list of items
2. Allow users to purchase items based on their product number
3. After the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remind
4. Can exit at any time, when you exit, print purchased goods and balances
Salary=int (Input ("Input your Salary:")) #输入工资
Balance=salary #将工资赋值给余额
products=["book", "Coffe", "Cake", "iphone"] #定义可购买商品
PRICE=[40,20,30,2000] #定义商品价格
Print ("number", "Products", "price") #输出商品列表第一行
Products_buy=[] #设置购物车商品列表
For I in range (len): #输出商品列表产品及价格
Print (i+1, "\t\t", Products[i], "\t\t", Price[i])
Print ("Q", "\t\t", "Quit") #输出 "q" Command "Quit" (exit)
While True:
Number = input ("Input your Products_number:")
If number!= "q": #若输入的不是 "Q", integer type, cycle to print purchased goods and balances
Number_=int (number)
If balance >=price[number_-1]:
Balance-= price[number_-1]
Products_buy.append (Products[number_-1])
Print ("Products_buy is", Products_buy, ", Balance is", balance)
Else
Print ("Insufficient balance!") ")
else: #输入的为 "Q" To print purchased goods and balances
Print ("Products_buy is", Products_buy, ", Balance is", balance)
Break
To run the program:
Python Basics (2)