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