python3 shopping cart

Python3 implements the shopping cart applet. After optimization, the program is divided into functions, and the file is used to register and verify the user name and password.

 
#! / usr / bin / env python 
#-*-coding: utf-8-*- 
# @Time: 2020/4/20 4:12 pm 
# @Author: hk 
# @Site: 
# @File: shopping cart. py 
# @Software: PyCharm 

"" " 
Description of requirements: 
    1. Enter the correct account number and password, enter the shopping cart to shop 
    2. Login failed, re-enter account password 
    3. The same account failed to log in more than three times, then lock the account 
    4. Login The account is locked, it prompts that the account is locked 
  "" "

#Display the 
purchased goods list function def shoping_list (myshping, money): 
    print ('============= Your shopping list is as follows ============== === ') 
    dic1 = {} 
    for i in myshping: 
        if dic1.get (i) == None: 
            dic1 [i] = 1 
        else: 
            dic1 [i] + = 1 
    for k, v in dic1.items () : 
        print (k, v) 
    print ("============================================ === \ nYour balance:% d "% money) 
#Shopping 
cart function def shoping (): 
    goods = [{'name': 'Computer', 'price': 1999}, 
             {'name': 'Mouse ',' price ': 10}, 
             {' name ':' Yacht ',' price ': 20}, 
             {' name ':' Beauty ',' price ': 998},
             {'name': 'iphone', 'price': 5000},

             ] 
    flag = False 
    my_shopping = [] 
    while True: 
        money = input ("Please enter your amount: \ n") 
        if not money.isdigit (): 
            keys = input ("Wrong input, please press any key to re-enter, or Enter q to exit the purchase \ n ") 
            if keys.upper () == 'Q': 
                break 
            continue 
        money = int (money) 
        while True: 
            print ('+++++++++++ Product list +++ ++++++++++ ') 
            for index, key in enumerate (goods): 
                print (index + 1, key [' name '], key [' price ']) # The value obtained by key here is Each row of the dictionary, and then press the key value of the key dictionary to print out the product details 
            print ('my wallet balance', money) 
            num = input ("Please enter the serial number of the product you want to buy: [or enter q to end the purchase] \ n ")
            if num.upper () == 'Q': 
                flag = True
                break 
            if not num.isdigit (): 
                print ("Input error, please re-enter!") 

                continue 
            num = int (num) 
            if num> 0 and num <len (goods ) + 1: 
                # print (num, "Start shopping \ n ----------") 
                if money> = goods [num-1] ['price']: 
                    my_shopping.append (goods [num- 1] ['name']) 
                    money = money-goods [num-1] ['price'] 
                    shoping_list (my_shopping, money) 
                    # print (my_shopping, "My Balance", money) 
                else: 
                    # print ("Insufficient balance ") 
                    while True:
                        chongzhi = input ("Insufficient balance, please enter the recharge amount: [or enter q to end the purchase.] \ n")
                        if chongzhi.upper () == "Q": 
                            flag = True 
                            break 
                        if not chongzhi.isdigit (): 
                            # keys_1 = input ("The input is incorrect, please re-enter the recharge amount: or enter q to end the purchase ... Error] ") 
                            # if keys_1.upper () ==" Q ": 
                            # flag = True 
                            # break 
                            print (" Input error, please re-enter ") 
                            continue 
                        money = money + int (chongzhi) 
                        break 
                    # break 

                    if flag == True:
                        break 
            else: 
                print ("Please enter the correct product number") 
                continue 
        if flag == True: 
            # Print out the shopping list and make statistics, showing the balance. 
            print ("Thank you for coming!") 
            shoping_list (my_shopping, money) 
            exit (0) 
        continue 
#Registration 

function def regist (): 
    "" " 
    Registered user 
    name: User name 
    password: User password 
    : return: 
    " "" 

    # Read user File 
    with open ("user.txt", encoding = 'utf-8', mode = "r") as read_file: 
        read_data = read_file.
            i_line = i.strip (). split (',') 
        while True: 
            username = input ('Please enter user name: \ n'). strip () 
            if username in i_name: 
                print ('User name already exists, please retry Enter. ') 
                Continue 
            else: 
                password = input (' Please enter the password: \ n '). 
                Strip () with open (' user.txt ', encoding =' utf-8 ', mode =' a ') as read_f1: 
                    new_user = [username, password, '0'] 
                    new_user = '\ n' + ','. join (new_user) 
                    # print (new_user, type (new_user)) 
                    read_f1.write (new_user) 
                    print ('% s, congratulations Successful registration, please log in '%username)
                    break 
number of user logins The function 
def modify_user (name,mode="add"):
    """
    Modify the number of login errors in the user list 
    : param name: user name 
    : param mode: add, the number of login errors is incremented by 1; del, the number of login errors is reset to 0 
    : return: 
    "" " 

    # read user files 
    with open ( "user.txt", encoding = 'utf-8', mode = "r") as read_file: 
        read_data = read_file.readlines () 

    # Write user file 
    with open ("user.txt", encoding = 'utf-8 ', mode = "w") as write_file: 
        for line in read_data: 
            new_line = line.split (', ') 
            if new_line [0] == name: 
                if mode == "add": 
                    new_line [2] = int ( new_line [2]) + 1 
                    new_line [2] = str (new_line [2])
                    new_line[2] = "0"
                    print(">>>>>>>",new_line)
                else:
                        print ("Dear [% s], you are welcome to log in to this shopping cart "% name)
                new_line = ",".join(new_line) + "\n"
                write_file.write(new_line)
                # print('写入',new_line)
                write_file.flush()
            else:
                write_file.write(line)

#验证用户名密码函数
def auth(name, passwd):
    with open("user.txt", encoding='utf-8',mode="r") as read_file:
        for eachLine in read_file:
            user_name, password, errors = eachLine.strip().split(',')
            if name == user_name:
                if int(errors) < 3:
                    if password == passwd:
                        modify_user(name, "del") 
                        shoping () 
                        # return True 
                    else: 
                        print ("Password is wrong, please re-enter username and password!") 
                        modify_user (name, "add") 
                        return False 
                else: 
                    print ("Your user is locked, please Contact the administrator! ") 
                    Return False 
        else: 
            print (" Username does not exist, please re-enter! ") 
            Return False 


def login (): 
    while True: 
        name = input (" Please enter user name: ") 
        passwd = input ( "Table Enter Password:") 
        if auth (name, passwd):
            break
def menu() :
    while True:
        mode = input ('Please register or log in: \ n' 
                    'Registration selection 1 \ n'
                    'Login select 2 \ n' 
                     'or press q to exit \ n'). strip () 
        if mode == '1': 
            regist () 
        elif mode == '2': 
            login () 
        elif mode.upper () == 'Q': 
            exit (0) 
        else: 
            print ("Input error, please re-enter, or press q to exit.") 
            Continue 



if __name__ == '__main__' : 
    menu ()

user.txt file content

hp, 123.2

abc,222,2

haha,1,1

test,1234,0

 

Guess you like

Origin www.cnblogs.com/amhk/p/12752704.html