Python enables simple instance sharing of shopping carts

Source: Internet
Author: User
The following small series for everyone to bring a Python beginner shopping cart Program Practice example (recommended). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

Nonsense not much to say directly on the code


#Author: Lancy wuproduct_list=[(' Iphone ', 5800), (' Mac Pro ', 9800), (' Bike ', ' + '), (' Watch ', 10600), (' Coffee ', ' + '), (' Lanc Y Python ', +)] #商品列表shopping_list =[] #定义一个列表来存储已购商品salary =input ("Please enter Salary:") if Salary.isdigit (): #当输入的内容为数字 salary=int (salary) #将输入的工资转换成int类型 while True: #循环打印出所有商品列表, there are two ways of writing, typically with the following # for item in Product_list: # print (Product_list.index (item), item) for Index,item in Enumerate (product_list): #enumerate () This method is to take out the list subscript print (Index,item) user_choice=input (" Do you purchase goods? If you want to buy a product please enter the Product code: ") If User_choice.isdigit (): #当输入的商品编号为数字 user_choice=int (user_choice) #将输入的商品编号转换成int类型 if User_choic E<len (product_list) and user_choice>=0: #判断输入的商品编号是否存在 P_item=product_list[user_choice] #根据商品下标取出所购买的商品 if p_i Tem[1]<=salary: #当商品的价格小于等于余额 shopping_list.append (P_item) #将购买的商品存储到shopping_list [] list salary-=p_item[1] #计 Balance print ("The item you purchased is%s, balance is \033[31;1m%s\033[0m"% (p_item,salary)) #这个地方做了一下处理, highlight the balance to else: #当商品的价格大于余额 print ("\033 [41;1m your balance remains [%s]\033[0m]%salary) #这个地方做了一下处理, highlight the balance else:print ("The item does not exist! ") elif user_choice== ' Q ': #当输入的商品编号为q时, print the purchased goods and balances and exit the program print ("--------The following items are purchased--------") for p in Shopping_list:p Rint (p) print ("Your balance is:", salary) exit () Else:print ("The item does not exist!") ")
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.