#-*-coding:utf-8-*-#Author:sonnarbalance = Int (input ("Please enter salary")) List_of_goods=[['Nioxin', 188],['Mayoou', 98],['Liese', 71],['Aerogard', 69], ['AMOS', 139],['Tide', 29],['Crest3d', 219],['AEON', 60], ['Morocanoil', 298],['Emart', 128],['Nioxin', 210],['Fino', 118]]Print("Welcome to the shopping system ...")Print("---------List of goods------------") forIndex,iteminchEnumerate (list_of_goods):Print(Index,"\ t", item)Print("---------END of Goodslist------------")Print("\033[1;32;0mplease Enter the number or the goods you want to buy...\033[0m")Print("\033[1;31;0mif you don ' t want to buy it,you can enter ' Q ' leave...\033[0m") Shopping=trueshopping_list=[] whileShopping:user_choice=input ("Enter you choice \ n") ifuser_choice.isdigit (): User_choice=Int (user_choice)ifUser_choice<len (List_of_goods) andUser_choice>=0:#prevent data from exceeding the listshopping_list.append (List_of_goods[user_choice]) temp=Balance Balance=temp-list_of_goods[user_choice][1] ifBalance<0:#when the balance is insufficientShopping_list.pop () Balance=temp#Save the last balance Print("Your Balance is not enough ...") Shopping=FalseElse: Print("Your have bought", List_of_goods[user_choice][1])#print a purchased item elifuser_choice=="Q": Shopping=FalseElse: Print("---------List of ShoppingCart------------") forIndex,iteminchEnumerate (shopping_list):Print(Index,item)Print("---------END of ShoppingCart------------") Print("Your balance is [%s]"% (balance))
Run with Python 3X to implement a small example of shopping by entering a number on the list of items. The condition of passing the while loop is false, exiting, or exiting the loop in case of insufficient balance. After exiting the loop, print a list of the items you purchased.
Python Learning notes list usage exercises