Not much to say, directly on the code:
1 #author:lancy Wu2 3product_list=[4('Iphone', 5800),5('Mac Pro', 9800),6('Bike', 800),7('Watch', 10600),8('Coffee', 31),9('lancy Python', 120)Ten]#Product List OneShopping_list=[]#define a list to store purchased items ASalary=input ("Please enter your salary:") - ifSalary.isdigit ():#when the input content is a number -Salary=int (Salary)#convert the entered salary to int type the whileTrue: - #cycle print out all the product list, there are two kinds of writing, generally with one of the following - #For item in Product_list: - #Print (Product_list.index (item), item) + forIndex,iteminchEnumerate (product_list):#Enumerate () This method is to remove the list subscript - Print(Index,item) +User_choice=input ("Do you purchase goods? If you want to buy a product, please enter the product number:") A ifUser_choice.isdigit ():#when the product number entered is a number atUser_choice=int (User_choice)#converts the entered product number to an int type - ifUser_choice<len (Product_list) andUser_choice>=0:#determine if the entered product number exists -P_item=product_list[user_choice]#Take out the purchased item according to the product subscript - ifP_item[1]<=salary:#when the price of a commodity is less than or equal to the balance -Shopping_list.append (P_item)#Store purchased items in the shopping_list[] list -SALARY-=P_ITEM[1]#Calculate Balance in Print("the item you purchased is%s and the balance is \033[31;1m%s\033[0m"% (p_item,salary))#This place did a bit of processing, highlighting the balance - Else:#when the price of a product is greater than the balance to Print("\033[41;1m Your balance is left only [%s]\033[0m"%salary)#This place did a bit of processing, highlighting the balance + Else: - Print("the item does not exist! ") the elifuser_choice=='Q':#When you enter the item number Q, print the purchased item and balance and exit the program * Print("--------Below are the items purchased--------") $ forPinchshopping_list:Panax Notoginseng Print(P) - Print("your balance is:", salary) the exit () + Else: A Print("the item does not exist! ")View Code
Python Beginner Shopping Cart Program Practice Example