Python Shopping Cart

Source: Internet
Author: User

Python Shopping Cart

    • Demand

    1. The user name and password are stored in the file in the following format: xxx|xxx
    2. Start the program, log in first, log in successfully let the user enter the salary, and then print the product list, failed to log in again, more than three times to quit the program
    3. Allow users to purchase items based on their product number
    4. After the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remind
    5. You can exit at any time to print the purchased goods and balances when exiting

    • Flow chart

    • Python Code implementation
  1 #! /usr/bin/env Python 2 #-*-coding:utf-8-*-3 # Mall Shopping Cart 4 product_list = [5 [' Iphone7 Plus ', 6500], 6 [' Iphon E8 ', 8200], 7 [' MacBook Pro ', 12000], 8 [' Python Book ', p], 9 [' Coffee ', ' +], [' Bike ', 666], [' P En ', 2] Shopping_cart = [] + F = open (' User.txt ', ' r ') Lock_file = F.readlines () f.close () count=0 _list={} while true:20 if Count = = 3:21 Print ("User name input number reached 3 limit") Break all for I in Lock_file : I=i.strip () user_list[i.split (' | ') [0]] ={' password ': i.split (' | ')         [1]} user_name=input ("Please enter your user name >>:") if user_name not in user_list:28 print ("User name error") 29 Count+=1 if user_name in lock_file:31 print ("User name is locked, please contact administrator!") ") + exit () if user_name in user_list:34 user_password=input (" Please enter your password >>: ") If u            Ser_password = = user_list[user_name][' password ']: print ("Welcome to E-shop") 37 While true:38 salary = input ("Please enter your salary:") # Enter the amount of the If not Salary.isdigit (): # Determine the input Sal                 ARY is not a digital print ("Please enter the amount again because your input is not legal) # Enter an amount that is not legal continue 42 else:43 salary = Int (salary) # turns the number entered into the shape of the break, and True: Print (">> Welcome to E-Mart <<") for index, I in Enumerate (product_list): # Cycle Products  List, commodity list index print ("%s.\t%s\t%s"% (index, i[0], i[1]) # Print a list of items, display a list of items index choice =                     Input (">> Please enter the serial number or enter Exit to exit the mall >>"). Strip () If Len (choice) = = 0: # Determine if the input string is empty and string length 51  Print ('-you have not selected product <--') continue if Choice.isdigit (): # Judging the input choice is not a number choice = Int (choice) # Convert the input string to integer if choice < Len (P Roduct_list) and Choice>= 0: # The integer entered must be less than the number of items in the list Product_item = Product_list[choice] # Get Item 57 If salary >= product_item[1]: # Compare the existing amount with the commodity, whether you can afford the salary-= product_item[1] # after the merchandise Price Shopping_cart.append (product_item) # Add selected items to your shopping cart (                         "Add \033[32;1m%s\033[0m to Shopping cart, your current amount is \033[31;1m%s\033[0m"% (product_item[0], salary) 62                     else:63 print ("Sorry, your amount is insufficient, also poor \033[31;1m%s\033[0m"% (product_item[1]-salary,)) 64 else:65 print ("--No This item <--") elif choice = = "Exit": 6 7 Total_cost = 0 print ("Your Shopping Cart list:") for I in Shopping_car t:70 print (i) Total_cost + = i[1] Print ("Total Price of your cart Yes: \033[31;1m%s\033[0m "% (Total_cost,)) ("Your current balance is: \033[31;1m%s\033[0m"% (salary,)) break 75             Break else:77 Print ("Password error") + + 1 + if Count = 3:80 Print ("You have entered the wrong number of passwords 3 times and will lock your users!")             ") Bayi f = open (' Blacklist.txt ', ' W ') f.write ('%s '%user_name) F.close () 84 Break true:87 salary = input ("Please enter your salary:") # Enter the amount of the If not                     Salary.isdigit (): # To determine if the input salary is not a digital print ("Please enter the amount again because your input wage is not legal") # Enter the amount of illegal 90 Continue else:92 salary = Int (salary) # turn the number of inputs into shaping the Brea K 94 while true:95 print (">> Welcome to E-Mart <<") for index, I in ENU Merate (product_list): # Cycle Product list, commodity list index print ("%s.\t%s\t%s"% (index, i[0], i[1]) # Print a list of items, display a list of items index 98 choice = input (">> enter the serial number or enter Exit to exit the mall >>:"). Strip () Len (choice) = = 0: # Determine if the input string is empty and the string length is ('--you have not selected commodity <--') 101 Continu e102 if Choice.isdigit (): # Determine if the input choice is a number 103 choice = Int (choice) # Convert the input string to integer                         104 If Choice < Len (product_list) and Choice >= 0: # The integer entered must be less than the number of items in the list 105 Product_item = Product_list[choice] # get Merchandise 106 If salary >= product_item[1]: # Compare the existing amount with the commodity, whether Affordable 107 Salary-= product_item[1] # The price of the product is deducted 108 Shopping_cart.appen                             D (Product_item) # Add the selected item to the shopping Cart 109 print ("adding \033[32;1m%s\033[0m to Cart, 110                             Your current amount is \033[31;1m%s\033[0m "% (product_item[0],salary)) 111 else:112 Print ("Sorry, your amount is not enough, still poor \033[31;1m%s\033[0m "% (product_item[1]-salary,)) 113 else:114 Print ("-- No this item <--") elif choice = =" Exit ": Total_cost = 0117 Print                         ("Your Shopping Cart list:") 118 for I in shopping_cart:119 print (i) 120 Total_cost + = i[1]121 print ("Your shopping cart Total Price is: \033[31;1m%s\033[0m"% (Total_cost,)) 122 p Rint ("Your current balance is: \033[31;1m%s\033[0m"% (salary,)) 123 break
View Code

Python Shopping Cart

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.