Python Practice _ Shopping Cart (Simple edition)
Demand:
- Write a Python shopping cart to enter user initialization amount
- Product can be printed and the user enters the number to purchase the item
- Calculate user balances When shopping, whether you can purchase items
- Print a shopping ticket when you exit the checkout
The following code implements the functions and ideas:
Function:
(1) Budget amount control, can only enter a number greater than 0
(2) Product format printing
(3) Choose to complete the goods to buy, prompting the user to confirm again, after confirmation to start calculating whether the user balance is greater than or equal to the commodity price, the price is correct after adding to the shopping cart
(4) The input q is settled, the duplicate items are merged, the number is displayed, and the total consumption and balance are calculated.
Ideas:
(1) Product printing through nested list implementation
(2) The purchase of goods is only necessary to compare the user balance and the price of the commodity, if the product than the amount of a fight to add this product to a list
(3) Calculate the user's shopping cart list at checkout
Flow chart:
How to use:
Execution Environment: Python3.5
Execute the method and execute the execution.
Code:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 Import Time5 6List_items = [["iPhone4", 100],["IPhone5", 200],["IPhone6", 300],["IPhone7", 4000],["Python", 10000],]7User_shopping_cart = []8 9 defIn_money ():Ten " " One function to determine user input amount A " " - GlobalUser_in_money - whileTrue: theUser_in_money = input ("Please enter initial funds:"). Strip () - ifuser_in_money.isdigit (): - ifInt (User_in_money) >0: - whileTrue: + print_lists () - Else: + Print("\033[31minput error!\033[0m") A Else: at Print("\033[31minput error!\033[0m") - - defprint_lists (): - " " - get the number entered by the user, call the billing module - : return: in " " - Print("Product List". Center (40,"-")) toSpaces =" "* * + forProduct_infoinchList_items: -underlined = 20-Len (product_info[0]) the Print(Spaces,list_items.index (Product_info) +1,spaces,product_info[0],"."*underlined,product_info[1]) * Print("-"*40) $ Panax Notoginsengin_numbering = input ("Please enter the product number,[q]exit billing:"). Strip () - ifin_numbering.isdigit (): the ifInt (in_numbering) > 0 andInt (in_numbering) <=Len (list_items): + transaction_calculations (in_numbering) A Else: the Print("\033[31mthe Item number does not exist!\033[0m") + Else: - ifIn_numbering = ="Q": $ settlement () $ Else: - Print("\033[31minput error!\033[0m") - the deftransaction_calculations (numbering): - " "Wuyi Add the shopping cart module to determine if the user balance is sufficient to buy the product the " " - GlobalUser_in_money WuUser_in_money =Int (User_in_money) -numbering =Int (numbering) AboutPu_confirmation = input ("Product \033[32m%s\033[0m Whether to add to Cart (y/n):"%list_items[numbering-1][0]). Strip () $ ifPu_confirmation = ="y": - ifUser_in_money >= list_items[numbering-1][1]: -User_shopping_cart.append (list_items[numbering-1]) -User_in_money = user_in_money-list_items[numbering-1][1] A Print("Product%s Added Cart, current Balance%s¥"%( List_items[numbering-1][0],user_in_money)) + Else: the Print("the balance is insufficient, the commodity price \033[31m%s\033[0m¥,lacks \033[31m%s\033[0m¥"% (list_items[numbering-1][1],list_items[numbering-1][1]-User_in_money)) - Else: $ Print("\033[31mnot added to cart\033[0m") the the defsettlement (): the " " the Billing Module - " " in ifLen (user_shopping_cart) = =0: the Print("Shopping Cart There is no products, thank you patronage goodbye") the exit () About Else: the Print("Shopping list". Center (50,"-")) theConsumption =0 theNew_user = [] +[New_user.append (i) forIinchUser_shopping_cartif notIinchNew_user] - forUser_cartinchNew_user: theNumber =User_shopping_cart.count (User_cart)BayiSettlement_un = 15-Len (user_cart[0]) theTo_settlement_un = 25-settlement_un-len (str (user_cart[1]))-Len (user_cart[0]) theConsumption + = user_cart[1]* Number - Print(" "*5,user_cart[0],"."*SETTLEMENT_UN,USER_CART[1],"¥","."*to_settlement_un,"%s a"%(number)) -Times = Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime (Time.time ())) the Print(Times.center (50,"-")) the Print("shopping cost \033[32m%s\033[0m¥, current balance \033[32m%s\033[0m¥,thank patronize!". Center (50,"-")%(Consumption,user_in_money)) the exit () the - if __name__=="__main__": theIn_money ()
Shopping Cart
Python Practice _ Shopping Cart (Simple edition)