#Author:leonproduction_list= [ ('iphone', 5800), ('Mac Pro', 9800), ('Bike', 800), ('Watch', 10600), ('Coffee', 31), ('Alex Python', 120)]shopping_list=[]#Create an empty list to store the purchased items. Salary= Input ("Input your Salary:")#Enter PayrollifSalary.isdigit ():#determine whether the entered salary is a numbersalary = Int (salary)#The wage entered is a number set, and the wage is converted to an integral type. whileTrue: forIteminchProduction_list:#The first method of removing the Mark Print(Production_list.index (item), item)#For Index,item in Enumerate (production_list): #第二种取下标方法 #print (Index,item)User_choice = input ("Select the item to buy >>>:") ifUser_choice.isdigit ():#determine if the data is a numberuser_choice= Int (user_choice)#if it is data, turn the data into an integer type. ifUser_choice < Len (production_list) andUser_choice >=0:#Gets the list length, returns the number, which is the list lengthP_item=production_list[user_choice]#get the item and assign it to P_item ifP_item[1] <= Salary:#get the price of the item and determine whether the price of the product is less than or equal to the wageShopping_list.append (P_item)#conditional, put the purchased items into an empty listSalary-=p_item[1]#subtract the salary from the price of the goods purchased, the result is assigned to salary, the result is the remaining balance Print("Added%s To shopping cart, you are current balance is%s"%(p_item,salary))Else: Print("\033[31;1m your balance is insufficient, only%s.\033[0m left"%salary)Else: Print("\033[31;1m goods do not exist \033[0m") elifUser_choice = ='Q': Print("-----------Shopping list----------") forPinchshopping_list:Print(P)Print("your current balance:", Salary) exit ()Else: Print("Invalid option")
Python class notes-shopping Cart