Python practice-jump out of multi-layered loops and shopping carts

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

1234567891011121314 defwork():    forinrange(5):        print("i=", i)        forinrange(5):            print("--j=", j)            forinrange(5):                if k<2:                    print("------>k=", k)                else:                    return i,j,kprint(work())

  




Note: The following updates are extended for this link:

Http://www.cnblogs.com/xuyaping/p/6639830.html

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

1234567891011121314151617181920212223 salary=int(input("input your salary:"))                   #输入工资balance=salary                                            #将工资赋值给余额products=["book","coffe","cake","iphone"]                 #定义可购买商品price=[40,20,30,2000]                                     #定义商品价格print("number  ","products  ","price")                    #输出商品列表第一行products_buy=[]                                           #设置购物车商品列表forinrange(len(products)):                            #输出商品列表产品及价格    print(i+1,"\t\t",products[i],"\t\t", price[i])    print("q","\t\t","quit")                                  #输出“q”指令打印命令“quit”(退出)whileTrue:    number =input("input your products_number:")        ifnumber!="q":                                       #若输入的不是“q”,则为整型,循环打印已购买商品和余额        number_=int(number)        ifbalance >=price[number_-1]:            balance -=price[number_ -1]            products_buy.append(products[number_-1])            print("products_buy is",products_buy,",balance is ",balance)        else:            print("余额不足!")    else:                                                                       #输入的为“q”,打印已购买商品和余额        print("products_buy is", products_buy, ",balance is ", balance)        break

  

To run the program:

Python practice-jump out of multi-layered loops and shopping carts

Related Article

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.