Python Shopping Cart Applet

Source: Internet
Author: User

Requirements:
1. User input after payroll display product List
2. Select items by Product number
3. Print the list of items and the remaining wages after selecting the item
The code is as follows:
# Coding=utf-8
Product_list = [
(' iphone ', 5800),
(' Mac Pro ', 9800),
(' Bike ', 800),
(' Watch ', 10600),
(' Coffee ', 31),
(' Alex python ', 120),
]
Shopping_list = [] #购物车
Salary = input ("Input your Salary:")
If Salary.isdigit (): #判断工资是否为数字
salary = Int (salary)
While True:
For Index,item in Enumerate (product_list): #enumerate--Remove the label
Print (Index,item) #打印商品列表
#取下标print (Product_list.index (item), item)
User_choice = input ("Select commodity >>>:")
If User_choice.isdigit (): #判断用户输入是否为数字
user_choice = Int (user_choice)
If User_choice < Len (product_list) and User_choice >=0: #判断用户输入的商品编号是否在下标范围之内, Len ()-Fetch list subscript length
P_item = Product_list[user_choice] #通过下标获取商品价格

If p_item[1] <= salary: #买的起
Shopping_list.append (P_item) #将该商品加到购物车
Salary-= p_item[1] #扣钱
Print ("Added%s into shopping cart,your current banlance is \033[31;1m%s\033[0m"% (P_item, salary))
else: #买不起
Print ("\033[41;1m your balance remains [%s], you can not afford it, you may choose another product or enter ' Q ' exit \033[0m"% salary)
else: #输入编号不存在
Print ("Product does not exist [%s]", User_choice)

elif User_choice = = ' Q ': #退出
Print ("---------shopping list----------") #打印商品清单
For P in Shopping_list:
Print (P)
Print ("Your salary balance ... ", salary)
Exit ()
Else
Print ("Invalid option")

Python Shopping Cart Applet

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.