Requirements to implement features:
After starting the program, the user enters the payroll and then prints the list of items
Allow users to purchase items based on their product number
After the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remind
You can exit at any time to print the purchased goods and balances when exiting
Product_list = [(' iphone ', 8100),
(' Mac Pro ', 13000),
(' Sea food ', 600),
(' Bed ', 3200),
(' Chair ', 123),
(' Blue tooth Header ', 1800)
]
Shopping_list = []
Salary = input ("Please input your salary here:")
If Salary.isdigit (): #判断工资是不是数字
salary = Int (salary) #工资是数字, change the salary type to int
While True: #进入循环
For index, item in enumerate (product_list):
Print (index, product_list)
User_choice = input ("Please put number to choice, what do you want>>>>>>:")
If User_choice.isdigit (): #判断用户输入必须是数字
user_choice = Int (user_choice)
If User_choice < Len (product_list) and User_choice >= 0: #用户选择的数字小于product list length and greater than or equal to 0
P_item = Product_list[user_choice]
If p_item[1] <= salary:
Shopping_list.append (P_item) adds the item #shopping list
Salary-= p_item[1] #购买物品后在工资里扣除相应的钱
Print ("You have added%s to your shopping cart, and your current balance is: \033[31;1m%s\033[0m"% (p_item,salary))
# \033[31;1m%s\033[0m font color changed to red
Else
Print ("\033[31;1m you don ' t has enough salary!\033[0m")
Else
Print ("%s is no exist option!"%user_choice)
elif User_choice = = ' Q ':
Print ("----------------Your shopping list---------------")
For P in Shopping_list:
Print (P)
Print ("Your balance is:", salary)
Exit ()
Else
Print ("Invalid option")
With the for index, the item in enumerate (product_list) makes the product_list more flexible and can be added later to remove the inside element without affecting the original code
Note: This is the case when the list is printed: (not yet resolved)
0 [(' iphone ', 8100), (' Mac Pro ', 13000), (' sea food ', +), (' Bed ', 3200), (' Chair ', 123), (' Blue tooth Header ', 1800)]
1 [(' iphone ', 8100), (' Mac Pro ', 13000), (' sea food ', +), (' Bed ', 3200), (' Chair ', 123), (' Blue tooth Header ', 1800)]
2 [(' iphone ', 8100), (' Mac Pro ', 13000), (' sea food ', +), (' Bed ', 3200), (' Chair ', 123), (' Blue tooth Header ', 1800)]
3 [(' iphone ', 8100), (' Mac Pro ', 13000), (' sea food ', +), (' Bed ', 3200), (' Chair ', 123), (' Blue tooth Header ', 1800)]
4 [(' iphone ', 8100), (' Mac Pro ', 13000), (' sea food ', +), (' Bed ', 3200), (' Chair ', 123), (' Blue tooth Header ', 1800)]
5 [(' iphone ', 8100), (' Mac Pro ', 13000), (' sea food ', +), (' Bed ', 3200), (' Chair ', 123), (' Blue tooth Header ', 1800)]
But does not affect the purchase
Python Shopping Cart