用函数完成登录注册以及购物车的功能。

用函数完成登录注册以及购物车的功能。
1,启动程序,用户可选择四个选项:登录,注册,购物,退出。
2,用户注册,用户名不能重复,注册成功之后,用户名密码记录到文件中
用户名的要求是只能含有字母数字不能含有特殊字符。
密码的要求是长度不能超过14个字符。
3,用户登录,用户名密码从文件中读取,进行三次验证,验证不成功则退出整个程序。
4,用户登录成功之后才能选择购物功能进行购物,购物功能:
购物车的要求:
1,将购物车封装到一个函数中。
2,在上周的基础上,可以实现充值的拓展,以及其他相关拓展。
3,用户选择退出购物车时,将已购买的商品记录到一个文件中,将未付钱的但加入购物车的商品记录到一个文件中。
4,每个用户专属两个文件,一个文件(文件名:登录的用户名_buy_goods)存储已购买的商品,另一个文件(文件名:登录的用户名_want_buy)存储加入购物车但没有购买的文件。(升级功能)
5,给购物车增加两个功能:1,可查看已购买的商品。2可查看上次退出时加入购物车未购买的商品。(升级功能)
6,对于第5题的第二个功能可以进行购买商品并追加到已购买的文件中。(升级功能)
5,退出则是退出整个程序。

def regist():
    flag = True
    while flag:
        username = input('Please input your regist name(only characters or digits): ').strip()
        if username.isalnum():
            with open('Regist Information', encoding='utf-8', mode='r+') as f:
                lis = f.readlines()
                for i in lis:
                    if username == i.split('|')[0].strip():
                        print('Username is already exist,enter another one!')
                    else:
                        password = input('Input your password,lenth of the password is less than 15:')
                        if len(password) < 15:
                            f.write('{:<20} | {:<20}\n'.format(username, password))
                            print('Success Regist!')
                            flag = False
                            break
                        else:
                            print('Lenth of password is less than 15,enter your password again!')

        else:
            print('Username only contains characters or digits, please enter another username!')

def login():
    n = 0
    flag = True
    while flag and n < 3:
        username = input('Please input your username:').strip()
        password = input('Please input your password:').strip()
        with open('Regist Information', encoding='utf-8', mode='r') as f:
            lis = f.readlines()
            for i in lis:
                if username == i.split('|')[0].strip() and password == i.split('|')[1].strip():
                    print('Successful Login!')
                    flag = False
                    return username
            else:
                print('Wrong username or password,please enter again!')
                n += 1

def shop_car():
        goods = [{"name": "Dior Lipstick Colour 999", "price": 280},
                 {"name": "Mac Blush Colour 054", "price": 220},
                 {"name": "Urbandecay Eyeshadow Naked 3", "price": 370},
                 {"name": "Mac Blush Colour 584", "price": 140},
                 {"name": "Makeup Forever Brush 846", "price": 110},
                 {"name": "Chennal Perfume 864", "price": 590},
                 {"name": "Lancon Perfume Rose", "price": 650}]
        dic_money = {'余额': 0}
        print('欢迎光临,您现有余额为: {}元'.format(dic_money['余额']))

        money = float(input('请输入你要充值的金额: ').strip())
        dic_money['余额'] = dic_money['余额'] + money
        print('您充值之后现有余额为: {}元'.format(dic_money['余额']))

        print('商品列表'.center(20, '-'))
        for i in range(len(goods)):
            print(i + 1, '\t', goods[i]['name'], '\t', goods[i]['price'])
        print('b    购物车结算')
        print('-' * 20)

        goods_price = {}
        for i in range(len(goods)):
            goods_price[(goods[i]['name'])] = goods[i]['price']

        shop_carc = {}
        shop_carp = {}
        shop_list = []

        while 1:
            n = input('input your choice(1- {}) or input q(Q) to quit: '.format(len(goods))).strip()
            command = n
            if n.isdigit():
                if int(n) <= len(goods):  # 打印商品名称及商品价格,并将此商品,添加到购物车,用户还可继续添加商品
                    shop_name = goods[int(n) - 1]['name']
                    shop_price = goods[int(n) - 1]['price']
                    print('您本次选择的商品名称:{},价格:{}元'.format(shop_name, shop_price))
                    shop_list.append(shop_name)
                    shop_carc[shop_name] = shop_list.count(shop_name)
                    shop_carp[shop_name] = shop_carc[shop_name] * goods_price[shop_name]
                    print('\n' + '------您已选择的各商品清单-------' + '\n')
                    for item in shop_carc.items():
                        if shop_carc[item[0]] > 0:
                            print('名称--{} , 总数量--{},总价格--{}元'.format(item[0], shop_carc[item[0]], shop_carp[item[0]]))
                else:
                    print('输入有误,请重新输入')

            else:
                if command.lower() == 'y':  # 依次显示用户购物车里面的商品,数量及单价
                    print('您最终购买的商品'.center(20, '-'))
                    for key in shop_carc.keys():
                        print('商品:{},数量:{},价格:{}元'.format(key, shop_carc[key], shop_carp[key]) + '\n')
                    print('-' * 30)
                    break
                elif command.lower() == 'q':
                    break
                else:
                    print('输入有误,请重新输入')

import os
while 1:
    choice = input('Please input your choice(login:l(L), regist:r(R)').strip()
    if choice.upper() == 'L':
        username = login()
        filenames = os.walk(r'D:\userIfo')[2]
        buy_goods = buy[0]
        want_buy_goods =
        if '{}_buy_goods'.format(username) not in filenames:
            with open('{}_buy_goods'.format(username), encoding='utf-8', mode='w') as f:
                f.write('{}\n'.format(buy_goods))
        else:
            with open('{}_buy_goods'.format(username), encoding='utf-8', mode='a+') as f:
                f.write('{}\n'.format(buy_goods))
        if '{}_buy_goods'.format(username) not in filenames:
            with open('{}_want_buy'.format(username), encoding='utf-8', mode='w') as f:
                f.write('{}\n'.format(want_buy_goods))
        else:
            with open('{}_want_buy'.format(username), encoding='utf-8', mode='a+') as f:
                f.write('{}\n'.format(want_buy_goods))

        choice2 = input('Please input your choice(buy:b(B),quit:q(Q)').strip()
        if choice.upper() == 'B':
            pass
        elif choice.upper() == 'Q':
            break
        else:
            print('Wrong input,Please enter the right choice!')
    elif choice.upper() == 'R':  # 注册功能
        regist()

    else: print('Wrong input,please input right choice')

猜你喜欢

转载自blog.csdn.net/weixin_42233629/article/details/81951303