Write a shopping cart program in python

Source: Internet
Author: User

Write a shopping cart program in python
1. You are not required to enter the item to determine whether the user has an amount. 2. The item list is displayed. You can enter the item number, shopping cart display, balance display, and settlement as needed; 3. (1): Enter "free" to display the remaining user amount. (2) Enter "list" to display the shopping cart list. (3) Enter "all" to settle the account. settlement: 1. check whether there are items in the user's shopping cart, and exit if there are items in the shopping cart. 2. If there are items in the shopping cart, the user will be prompted whether to buy: Y or y: the purchased items, and the cost and remaining items are displayed; N or n: exit, clear the shopping cart, display the balance, and exit. Other inputs: exit, clear the shopping cart, display the balance, and exit. (4): Enter the product number: 1. Check whether the user's balance can be used to purchase the product. If the user cannot purchase the product, a prompt is displayed. 2. If the user can purchase the product, ask the user to enter the number of items to be bought, if the purchased quantity exceeds the user balance, you cannot purchase it. 3. the user's balance can purchase the corresponding item, add the item to the shopping cart, and display the item list. 4. Users can go to the shopping cart to settle items only when entering all. The Code is as follows:

#! /Usr/bin/env python #-*-coding: UTF-8-*-_ author _ = 'weibo INF '# define an empty shopping list: cart = [] # query user balance import osdef money (): filename = r 'C: \ Users \ Administrator \ Desktop \ money.txt 'if OS. path. exists (filename): m = open ('C: \ Users \ Administrator \ Desktop \ money.txt ', 'R') mon = m. readline () m. close () mon = int (mon) return mon else: exitt = False while not exitt: aa = raw_input ("\ 033 [31m Please enter your salary: \ 033 [0 m "). strip () if aa. Isdigit (): mon = int (aa) exitt = True else: print "enter the correct number .. "Return mon # Save your input balance def free_money (x): a = open ('C: \ Users \ Administrator \ Desktop \ money.txt ', 'w'). write ('% s' % x). close () # define the balance variable wage = money () wage1 = wage # define the total balance shoping_money3 = 0 # define the user's total consumption # define the product list: shop = [["iphone6s ", 5288], ["iphone6s plus", 6088], ["mac book", 8888], ["apple", 5], ["letv 1 S ", 1099] # display the shopping information cyclically: while True: # print the product list and price: for index, p in enumerate (shop): print index, p [0], p [1] # Get user input: shopin G = raw_input ("\ 033 [31m please enter the product number, enter all for settlement, enter: list for display shopping cart, enter: free: \ 033 [0 m "). strip () # determine whether user input is valid if shoping. isdigit (): shoping = int (shoping) shoping_money = shop [shoping] [1] if shoping_money <wage: # determine whether users can buy while True: num = raw_input ("Enter the quantity to purchase :"). strip () if not num. isdigit (): print "\ 033 [31m please enter the product quantity correctly .. \ 033 [0 m "else: num = int (num) if num = 0: break else: shoping_money2 = shoping_money * num # determine the price of the number of items if shoping_money2 <wage: # determine whether a user can buy cart. append (shop [shoping] [0]) # Add the product name to cart in the shopping cart. append (num) # add the item quantity to the shopping cart print "\ 033 [31m your item has been added to the shopping cart % s quantity: % s \ 033 [0 m "% (shop [shoping] [0], num) wage = wage-shoping_money2 # Calculate the remaining user amount shoping_money3 + = shoping_money2 # calculate the total user consumption amount break else: print "\ 033 [31 m your salary cannot afford % s % S \ 033 [0 m "% (num, shop [shoping] [0]) break elif shoping_money> wage: # The message "print" \ 033 [31m your salary cannot afford this item: % s \ 033 [0 m "% shop [shoping] [0] else: # tips for purchasing goods that cannot be purchased: print "\ 033 [31m. If your salary is too small, try to make money .. \ 033 [0 m "break elif shoping =" list ": # display the shopping list print "\ 033 [31 m ---------- shopping list ---------- \ 033 [0 m" num = 0 aa = len (cart) bb = aa/2 for x in range (bb): print cart [num], "\ 033 [31 m quantity: \ 033 [0 m ", cart [num + 1] num + = 2 print "\ 033 [31 m ----------------------------------- \ 033 [0 m" elif shoping = "free ": # Show remaining amount if not len (cart): print "\ 033 [31m you have not purchased, remaining: % s \ 033 [0 m" % wage else: print "\ 033 [31m total cost: % s, remaining: % s \ 0 33 [0 m "% (shoping_money3, wage) elif shoping =" all ": # user settlement if not len (cart ): # determine whether the shopping cart is empty print "\ 033 [31m your shopping cart is empty and no settlement is required .. \ 033 [0 m "free_money (wage) break else: # The shopping cart is not empty, display the shopping list and quantity of purchased items print "\ 033 [31 m ---------- shopping list ---------- \ 033 [0 m" num = 0 aa = len (cart) bb = aa/2 for x in range (bb): print cart [num], "\ 033 [31 m quantity: \ 033 [0 m ", cart [num + 1] num + = 2 print "\ 033 [31 m ----------------------------------- \ 033 [0 m" gm = raw_input ("\ 033 [31 m confirm Purchase: Y or N \ 033 [0 m "). strip () # determine whether the user has purchased the product if gm = "N" or gm = "n": # if the user does not buy print "You have not bought any product, and the remaining: \ 033 [31 m % s \ 033 [0 m "% wage1 free_money (wage1) break elif gm =" y "or gm =" Y ": # print purchased by the user "your total cost \ 033 [31 m % s \ 033 [0 m left \ 033 [31 m % s \ 033 [0 m" % (shoping_money3, wage) free_money (wage) break else: # incorrect input. print is not purchased by default. "You entered incorrect information and did not purchase any items. Remaining information: \ 033 [31 m % s \ 033 [0 m "% wage1 free_money (wage1) break

 


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.