Python shopping cart
1 msg_list = [ 2 ['iphone',8888], 3 ['coffe',38], 4 ['book',90], 5 ['Tesla',100000], 6 ['RR',1000000000] 7 ] 8 9 10 shopping_cart = [] 11 12 salary = int(input('input your salary: ' )) 13 14 while True: 15 #Counter , assign 16 to the items in the list index = 0 17 #loop the list msg_list, and print 18 for product in msg_list: 19 print (index,product) 20 index += 1 21 #Enter the item number to be selected 22 choice = input( ' >>: ' ).strip() 23 # Judge the type of input to see if it is a number, isdigit is the way to judge whether it is a number 24 if choice. isdigit(): 25 #Determine whether the input number is within the range of the index value of the list 26 choice = int(choice) 27 if choice >= 0 and choice <= len(msg_list): 28 #The product exists, get the product. 29 product = msg_list[choice] 30 #Determine whether the amount entered is affordable. 31 if product[1] <= salary: 32 #Affordable , and add it to the shopping cart. 33 shopping_cart.append(product) 34 #The total amount minus the money added to the shopping cart 35 salary -= product[1 ] 36 # \033[31;1m XXXXXXXX \033[0m Fixed writing, you can change the font color of XXXXXX 37 print ( ' Added product: ' + product[0] + ' into shopping cart, \033[31;1myour current\033[0m balance: ' + str(salary)) 38 else : 39 print ( ' Can't afford it , the product price is ' + str(product[1]) + " not yet " + str(product[1] -salary ) + ' money ' ) 40 else : 41 print ( ' product does not exist ' ) 42 elif choice == ' q ' : 43 print ( ' ------- Purchased items------- ' ) 44 45 for i in shopping_cart: 46 print (i) 47 48 print ( ' Your balance is: ' ,salary) 49 print ( ' --------- end---------- ' ) 50 break 51 else : 52 print ( ' no such option ' )