Product_list = [ ('MacBook', 9000), ('Kindle', 500), ('Tesla', 900000), (' Book', 100), ('Bike', 2000),]saving= Input ("Please input your money:") Shopping_car= []ifsaving.isdigit (): Saving=int (saving) whileTrue:#Print Product Content forI, VinchEnumerate (product_list, 1): PrintI">>>", V)#Guide users to select ProductsChoice = input ("choose goods that's want to buy[exit:q]:") #Verify that the input is legitimate ifchoice.isdigit (): Choice=int (choice)ifChoice > 0 andChoice <=Len (product_list):#Select a product to be selected by choiceP_item = product_list[choice-1] #If the money is sufficient, subtract the commodity price with saving and add the item to the shopping cart ifP_ITEM[1] <=saving:saving-= P_item[1] Shopping_car.append (p_item)Else: Print("-----------------") Print("Sorry, your balance is not enough.") Print("Your Balance:"+str (saving))Print("-----------------") Continue Print("-----------------") Print("You have chose"+p_item[0])Print("Your Balance:"+str (saving))Print("-----------------") Else: Print("Non existent") elifChoice = ='Q': Print("---------------") Print("You have chose the following goods:") Print("Goods\t\tnumber\tprice") Num= 1#Loop through the goods in the shopping cart, the shopping cart is stored in the purchased goods forIinchShopping_car:Print(I[0] +"\t\t"+ str (num) +"\t\t"+ STR (i[1])) Print() Print("Balance:", saving)Print("---------------") Break Else: Print("Invalid input")
Python small Code _3_ shopping Cart