Product_list = [
(' iphone ', 8000),
(' Mac Pro ', 9800),
("Bike", 3000),
("Watch", 10000),
("Cofee", 30),
("book", 100),
] #定义商品列表
Shoppint_list = [] #购物车空列表
While True:
Salary = input ("Input you Salary:") #输入预算
If Salary.isdigit (): #isdigit integer
salary = Int (salary)
While True: #循环
# for item in Product_list:
# Print (Product_list.index (item), item)
For Index,item in Enumerate (product_list): #下标做产品编号, enumerate out list subscript
Print (Index,item) #index下标, item data
User_choice = input ("Choose what 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:
Shoppint_list.append (P_item)
Salary-= p_item[1]
Print ("Added%s into shopping cart,your current balance is%s"% (p_item,salary))
Else
Print ("\033[41;1m your balance remains [%s], and buy a chicken leg \033[0m"% (Salary)) # "\033[[\033[om" plus bright color
Else
Print ("Product does not exist")
elif User_choice = = ' Q ':
Print ("----Product list------")
For P in Shoppint_list:
Print (P)
Print ("You balance:", salary)
Exit ()
Else
Print ("Invalid option")
Else
Print ("Please enter a number")
Python Shopping Cart