zoukankan      html  css  js  c++  java
  • 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}
    ')
    
    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]()
    
    
  • 相关阅读:
    字符编码总结
    文件操作总结(2)
    Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) C. Five Dimensional Points 暴力
    UVA 10048
    UVA 247
    UVA 1151
    UVA 1395
    Codeforces Round #260 (Div. 1) D. Serega and Fun 分块
    Codeforces Beta Round #13 E. Holes 分块
    Codeforces Round #404 (Div. 2) E. Anton and Permutation 分块
  • 原文地址:https://www.cnblogs.com/2222bai/p/11566800.html
Copyright © 2011-2022 走看看