Python Design Shopping Cart

Source: Internet
Author: User

                                                  Design Shopping Cart

A need:

1, after starting the program, enter the user name password, if it is the first time to log in, let the user enter wages, and then print the product list

2. Allow users to purchase goods according to the product number

3, the user selects the goods, checks whether the balance is enough, enough on the direct debit, not enough on the reminder

4, can withdraw at any time, exit, print purchased goods and balance

5, in the user process, the key output, such as the balance, the product has been added to the shopping cart and other messages, need to highlight

6, the user after the next login, enter the user name password, directly back to the last state, that is, the balance of the last consumption or those, re-login to continue to purchase

7, allow to query the previous consumption records

Two. Shopping Cart user information:

{' name ': {' password ': ' 10000 '}, ' cx ': {' 123 ': ' 725.0 '}, ' even ': {' qw ': ' 1560 '}, ' Cv1 ': {' 1 ': 100.5}}
Store user name, password, and balance in dictionary form

Three. Historical Shopping information:

{' name ': [[' iphone ', 5800], [' Bike ', ' + '], ' CX ': [[' iphone ', 5800], [' Apple ', 5], [' Apple ', 5], [' Book ', '  75] '}
In dictionary form, corresponding user name and historical shopping record

Four. Flowchart:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
# Author:even

set = False # Sets the setting to exit if the input is Q
File = open (' Shopping cart user Information archive ', ' r+ ', encoding= ' Utf-8 ') # Read cart user information files
f = str (File.read ()) # Converts the contents of a file into a string
For lines in F: # progressive Read
File_str = str (f)
Data= eval (file_str) # Converts a string to a dictionary data
Name = input ("Please enter Name:") # Prompt for user name
Password = input ("Please enter password:") # Prompt for password
While True:
If name in data: # User name in dictionary data
If password in data[name]: # password If you can apply the user name, you are welcome to login
Salay = float (Data[name][password])
Print ("\033[32;1m welcome login, Current balance is%s\033[0m"%salay)
Break
else: # Otherwise the password input error, prompt re-enter
Password = input ("Password input error, please re-enter:")
Continue
else: # Otherwise, the user name, password, and payroll will be stored in the user information file for the first login.
Password_salay = {}
SALAY_STR = input ("Welcome first sign In, please enter your salary:") # Enter the number of wages
Salay = Float (salay_str) # Converts the input string to a number
Password_salay[password] = salay # match wage to password
Data[name] = password_salay # The password-wage pair corresponds to the user name
File.seek (0) # file read move to start
File.write (data) # Write new dictionary information
File.tell () # returns to the current position
Break


list = [# shopping list
["iphone", 5800],
["Bike", 800],
["MacBook", 17500],
["book", 75],
["Apple", 5]
]

File_list_r = open (' History purchase record ', ' r+ ', encoding= ' Utf-8 ') # Read History purchase record file
F_list_r = str (file_list_r.read ())
Shoppinglist_dict = eval (f_list_r) # Convert history information to a dictionary
If name not in Shoppinglist_dict: # If you log in for the first time, there will be no history
Shoppinglist_dict[name] = [] # First login history left blank
Shoppinglist = Shoppinglist_dict[name] # is not the first login, assigning a previous history to a variable
Shoppinglist_now = [] # This shopping record is left blank
Choose = Input ("\ n Whether you need to query historical shopping Records (y/n):") # Ask if you need a query history
If choose = = ' y ': # Select Y, output historical shopping record
Print ("\ n \ nyou-----------History shopping------------")
Print (shoppinglist)
Print ("--------------end---------------\ n")

While not set: # Shopping Cart starts
Print ('-----------commodity List------------') # Output Commodity list
For Index,item in Enumerate (list,1):
Print (Index,item)
Print ("-------------End--------------")
Number = input ("Please enter the product code you want to buy:") # Enter the product number
If number = = "Q": # when input is Q, exit, print this shopping list
Set = True
Data[name][password] = str (salay) # assigns the wage of the string to the password in the name of the application user-the wage pair
File.seek (0)
File.write (data) # writes user information to the shopping cart user Information archive
File.tell ()
Print ("------------shopping list------------") # Prints the shopping list and prompts for the balance
Print (Shoppinglist_now)
Print ("Your balance is:", Salay)
Print ("-------------End--------------")
Shoppinglist.extend (shoppinglist_now) # append this shopping record to the list of shopping records
Shoppinglist_dict[name] = shoppinglist # shopping list corresponds to user name
File_list_r.seek (0)
File_list_r.write (str (shoppinglist_dict)) # Write the shopping history of the string
File_list_r.tell ()
Elif number.isdigit () = = False: # Verifies whether the input is an integer
Print ("\033[31;1m input is not a numbered content, please re-enter \033[0m")
elif Int (number) >int (len (list)) or int (number) <= 0: # If the input value is not in the list, prompt for an error
Print ("\033[31;1m the item you purchased is not in the list \033[0m")
Else
number_buy = Int (number)-1
If list[number_buy][1]< (salay): # If the balance is enough, prompt for a successful purchase, showing the balance
Salay = Salay-int (list[number_buy][1])
msg = "'
\033[32;1m you have added%s to your shopping cart
Balance is%d\033[0m
"% (List[number_buy][0],salay)
Print (msg)
Shoppinglist_now.append (List[number_buy]) # This shopping information is added to this purchase record
Else
Print ("\033[31;1m you have no balance to buy \033[0m") # Otherwise, the balance is insufficient

Python Design 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.