Python Basics (2)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.