Tiny_Lu
不忘初心

Day 11 简单购物车

user_info_list = []
shopping_car = {}
product_dict = {
    '0': "芭比娃娃",
    '1': "变形金刚",
    '2': "psp游戏机",
    '3': "奥特曼",
    '4': "遥控飞机",
    '5': "chongqiwawa",
}

def user_info_inp():
    user_name_inp = input('用户名:')
    user_pwd_inp = input('密码:')
    return user_name_inp, user_pwd_inp

def register():
        user_name_inp, user_pwd_inp = user_info_inp()
        with open('user_info.txt', 'a', encoding='utf8') as fa:
            fa.write(f'{user_name_inp}:{user_pwd_inp}\n')

def login():
    login_count = 0
    if user_info_list:
        print('您已登陆')
        return
    while login_count < 3:
        user_name_inp, user_pwd_inp = user_info_inp()
        with open('user_info.txt', 'r', encoding='utf8') as fr:
            for i in fr:
                user_name, user_pwd = i.split(':')
                print(user_name, user_pwd)
                if user_name.strip() == user_name_inp and user_pwd.strip() == user_pwd_inp:
                    print('登录成功')
                    user_info_list.append(user_name)
                    return
            else:
                print(f'密码错误,剩余错误次数{2-login_count}')
                login_count += 1


def logout():
    if not user_info_list:
        print('你未登录')
        return
    else:
        user_info_list.clear()

def shopping():
    if not user_info_list:
        print('你未登录')
        return

    print('''
    0 芭比娃娃
    1 变形金刚
    2 psp游戏机
    3 奥特曼
    4 遥控飞机
    5 chongqiwawa
    ''')

    product_choice = input('选择购买的商品:')
    if product_choice not in product_dict:
        print('请选择正确的商品')
        shopping()
    else:
        product = product_dict[product_choice]
        if product in shopping_car:
            shopping_car[product] += 1
        else:
            shopping_car[product] = 1
    print(f'你购买了商品{product}')

def pay():
    if not user_info_list:
        print('你未登录')
        return

    print('是否结算购物车(Y/N):')
    is_pay = input('>>')
    if is_pay == 'Y':
        shopping_car.clear()
    return


choice_dict = {
    '0': login,
    '1': register,
    '2': shopping,
    '3': pay,
    '4': logout
}



while True:
    print('''
    0 登录
    1 注册
    2 购物
    3 结算
    4 注销
    q 退出
    ''')

    choice = input('>>')
    if choice not in choice_dict:
        print('非法输入')
    elif choice == 'q':
        break
    else:
        choice_dict[choice]()

posted @ 2019-09-22 11:38  二二二二白、  阅读(119)  评论(0编辑  收藏  举报