author = "Superman"
product_list = [
(' Iphone ', 5800),
(' Mac Pro ', 9800),
(' Bike ', +),
(' Watch ', 10600),
(' Coffee '),
(' Alex Python ',
]
shopping_list = []
salary = input ("Input your Salary:")
if Salary.isdigit (): #判断工资是不是数字
salary = Int (salary) #如果是数字就用Int的数据类型
While True: #进入死循环
For Index,item in Enumerate (product_list): #打印商品列表
#print (Product_list.index (item), item)
print (Index,item)
User_choice = input ("Choose what you want to buy?") >>>: ")
if User_choice.isdigit (): #判断工资是数字类型
user_choice = Int (user_choice)
if User_choice < Len (product_list) and User_choice >=0: #判断商品元素数量
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 balance is \033[31;1m%s\033[0m"% (p_item,salary))
Else:
print ("\033[41;1m your balance remains [%s], and buy a wool \033[0m"% salary)
Else:
Print ("Product code [%s] is not exist!" % user_choice) #输入的商品序号不存在
elif User_choice = = ' Q ':
print ("--------shopping list------")
For p in shopping_list:
Print (p)
print ("Your Current balance:", salary)
exit ()
Else:
Print ("Invalid option")
The supermarket shopping cart system of Python face test