Alex Great God's shopping cart needs:
Product list, select items to add to the shopping cart, the deduction shows the balance, such as insufficient balance to prompt purchase of this item also need to recharge the amount, exit after the automatic checkout display balance;
#edited by: Shanlong#Weak chicken Shopping cart procedure, demand: Define the amount, select the product, do not choose to tell the user the remaining amount, Revenue Q exit#defines a money variable as a string and a memorial for the type of conversion that followsmoney="1";#first determine whether the type of money is int while(Type (money)! = Type (1)): #attempt to convert the amount entered by the user to int Try: Money=int (Input ("Please enter the amount you are carrying this time:")); #if the amount entered by the user cannot be converted to int, repeat the input except: Print("What kind of ghost are you typing?");#define the name of the commodity and the price of the commodity, and the price corresponds to the commodity one by one;Products = ["Candy","Apple","Banana","Cucumber","Eggplant"]; Productsprice= [15, 25, 20, 10, 5];#judge the money in your hand enough to buy the most of our home goods, not enough to please consciously leaveifMoney <min (productsprice):Print("you get some money, buy a sweater!"); Exit ();#start entering the mallPrint("Welcome to Fire Mall!\n we offer the following items for your selection:");#List of products selected by the userUserselectlist = [];#List of product prices selected by the userUserselectprice = [];#Start Cycle Mall Product List whileTrue:#output commodity list using for loop output forIinchRange (0, Len (products)):Print(Str (I+1) +". "+ Products[i] +"\ t"+str (productsprice[i])); #get user-selected itemsUserselect = input ("Please enter the number or name of the product you are purchasing (Q or roll out shopping cart):"); #to determine if it is not an option to exit, proceed if(Userselect! ="Roll" andUserselect! ="Q" andUserselect! ="Q") : #determine whether the user entered a product code or a product name: Try: #if the successful conversion of userselect to int means that the user entered the product code #-1 means the product display code differs from the position within the list 1userselect = Int (userselect)-1; #get the product name for the product code that the user has enteredUserproducts =Products[userselect]; #Select the price list for the item to add the price of the currently selected itemuserselectprice.append (Productsprice[userselect]); ##判断用户输入的是商品名称: except: #If the product name appears in the list of items, continue if(Products.count (Userselect) >0):#Select the price list for the item to add the price of the currently selected itemuserselectprice.append (Productsprice[products.index (userselect)); #get the name of the productUserproducts =Userselect; #if the item is not in the list, prompt and re-cycle Else: Print("excuse me, what kind of ghost are you typing?"); Continue; #determine if there is a product in the user's shopping cart, to calculate whether the balance is sufficient if(len (userselectlist) = =0):#add items directly without a productuserselectlist.append (userproducts); Else: #determine whether the product price combination of the user's choice is greater than the balance; if(SUM (Userselectprice) >Money ):Print("Sorry, your balance is not enough! If you want to buy", Userproducts,"also need to recharge", (SUM (userselectprice)-Money )); #If a product addition fails, the item that was last added to the user price list is deleted;Userselectprice.pop (-1); Else: #Add a product to a user's shopping cartuserselectlist.append (userproducts); Print("Current Balance:", Money-sum (Userselectprice),"Yuan"); Else: Break;Print("the items you purchased this time are as follows:"); forIinchRange (0, Len (userselectlist)):Print(Str (i + 1) +". "+ Userselectlist[i] +"\ t"+str (userselectprice[i]));Print("Total:", sum (userselectprice),"Yuan");Print("Your balance:", Money-sum (Userselectprice),"Yuan");
Python practice-The loneliness of a shopping cart is Alex's fault.