Product_list = [(' iphone ', 5800), (' iwatch ', 2400), (' book ', +), (' Apple ')] #创建列表库
Shoppinglist = [] #定义购物车的列表
Salary = input ("Input your Salary:")
If Salary.isdigit (): #判断输入的salary是否为数字
salary = Int (salary) #声明为整型
While True: #当为真
For Index,item in Enumerate (product_list): #获取列表里每个元组的数字排列值
# Print (Product_list.index (item), item) #打印列表中对应商品排序
Print (Index,item)
User_choice = Input ("Select the item to be purchased:")
If User_choice.isdigit ():
user_choice = Int (user_choice)
If User_choice < Len (product_list) and User_choice >=0: #判断选择的商品编号是否在列表库中
Price_item = Product_list[user_choice] #单品的价格对应的编号
Print (Price_item)
Print (price_item[1]) #单品价格
If price_item[1] <= salary: #单品价格小于用户余额
Shoppinglist.append (Price_item) #购物车添加对应的商品编号
Salary-= price_item[1] #salary minus the price of the purchased item number
Print ("added \033[31;1m%s\033[0m into shopping cart, your balance is \033[31;1m%s\033[0m"% (price_item,salary)) # Print out cart and balance
Else
Print ("\033[41;1myour current isn't enough,%s\033[0m"%salary) #购买的现金不足支付 and prints out the balance corresponding to if User_choice < Len (product_list ) and User_choice >=0:
Else
Print ("Product code [%s] is not exist"% User_choice) #打印出不存在商品列表的
elif User_choice = = ' Q ': Whether the #接user_choice is a number, if q executes the following corresponding if User_choice.isdigit (): Also or
Print ("----------shopping list----------")
For P in Shoppinglist: #循环打印购物车
Print (P)
Print ("Your current balance:", salary) #打印当前余额
Exit () #退出
Else
Print ("Invalid option") # Invalid input corresponds to if User_choice.isdigit ():
#elif salary is not
#print ("Welcome to buy Buy sale")
Python Simple Shopping cart Practice