Shopping program

# -*- coding: utf-8 -*-
"""
Created on Tue Sep 11 16:35:29 2018

@author: 车路历程
"""
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{" Name " : " Beauty " , " . Price " : 998 }, 
] 
'' ' 
1, after starting the program, enter your user name and password, allowing users to enter wages, and then print the Product List 
2, allows users to purchase by product number 
3 after the user selects a product, testing the balance is enough, enough to direct debit, is not enough to remind 
4, you can leave at any time, when you exit, print the purchase of goods and the balance 
5, the user process, key outputs, such as balance, commodity added to cart other news, the need to highlight the 
expansion needs: 
1, after a user logs in, enter your user name and password, directly back to the previous state, that is the last consumer balance or what those may continue to log in again Buy 
2, allowing the consumer records before the query 
'' ' 
# user name; lilu password is: 123 
# file name created for: purchase_records.txt 
# press q to quit 
Import Re
 Import os 

# automatically created in the current directory empty file purchase_records.txt
= desktop_path The os.getcwd () 
Full_Path = desktop_path + ' \\ ' + ' purchase_records ' + ' .txt ' 
File = Open (Full_Path, ' A + ' ) 
a file.write ( '' ) 
File.close () 

name = INPUT ( ' Please enter your name: ' ) .strip () 
Key = iNPUT ( ' enter password: ' ) .strip () 

IF key.isdigit ():
     IF name == ' Lilu ' and key == '123':
        print('您上次的选购状态为:')
        with open('purchase_records.txt', 'r') as f:
            print(f.read())
        with open('purchase_records.txt', 'r') as f:
            last_time_record_list = [i for i in f.readlines()]
            
        if len(last_time_record_list) != 0:
            salary = re.search("[0-9]+", last_time_record_list[-1]).group()
            purchase_list = re.search("[[].+[]]", last_time_record_list[-2]).group()[1:-1]\
                            .replace("'",'').replace(' ', '').split(",")
        else:
            purchase_list = []
            salary = input('请输入您的工资:').strip()
            
        if salary.isdigit():
            salary =int (salary)
             the while True:
                 Print ([(i [0], i [1] [ ' name ' ]) for i in the enumerate (Goods)]) 
                Choice = the INPUT ( ' request for an article number, select your need product: ' ) .strip ()
                 IF choice.isdigit () and 0 <= int (Choice) < len (goods): 
                    Choice = int (Choice)
                     IF the salary> = goods [Choice] [ " . price " ]:
                        purchase_list.append(goods[choice]["name"])
                        print('\033[1;31;40m%s\033[0m已加入购物车' %goods[choice]["name"])
                        with open('purchase_records.txt', 'a') as f:
                            left_salary = salary - goods[choice]["price"]
                            f.write('salary:'+str(salary)+'---'Goods + [Choice] [ " name ]
                                      + ' --- ' + ' left_salary: ' + STR (left_salary) + ' \ n- ' )              
                        the salary - = Goods [Choice] [ " . price " ]
                         Print ( ' balance: \ 033 [1; 31; 40m% S \ 033 [0m ' % salary)
                     the else :
                         Print ( ' \ 033 [1; 35; 40m less than your balance, please recharge \ 033 [0m ')
                 eLif" Choice == ' Q' :
                     Print ( ' you selected commodities:% S ' % purchase_list) 
                    with Open ( ' purchase_records.txt ' , ' A ' ) AS f: 
                        f.write ( ' Purchase Records:% S \ the n- ' % purchase_list) 
                        f .write ( ' account balance:% S \ the n- ' % salary)
                     BREAK 
                the else :
                     Print ( ' what you entered is incorrect, please re-enter ' )             
         the else :
             Print ( ' Please enter the number ' )
     the else :
         Print ( ' you enter a user name and password is incorrect, please re-enter ' )
 the else :
     Print ( ' Please enter a numeric password ' )

 

Guess you like

Origin www.cnblogs.com/czz0508/p/11100969.html