Python3 函数实践之简易购物系统

函数实践之简易购物系统

项目主要需求:

  • 用户可以自行选择功能

  • 该购物系统具有注册/登录/购物/购物车/退出登录功能

  • 用户在登录后才能使用购物/购物车/退出登录功能

'''
注册
登录
购物
购物车
退出登录
'''

username_list = []
goods_dict = {
    '1': '马云',
    '2': '马化腾',
    '3': '马冬梅'
}
shopping_cart_dict = {}


def register():
    '''注册'''
    print('*' * 20, '注册', '*' * 20)

    while True:

        username_inp = input('请输入用户名: ')
        pwd_inp = input('请输入密码: ')
        re_pwd_inp = input('再次输入密码以确认: ')

        if pwd_inp == re_pwd_inp:
            with open('user_info', 'a', encoding='utf-8') as fa:
                fa.write(f'{username_inp}:{pwd_inp}
')
                print('注册成功')
                break
        else:
            print('两次密码输入不一致')


def login():
    '''登录'''
    print('*' * 20, '登录', '*' * 20)

    count = 0
    while count < 3:

        if username_list:
            print('你已登录')
            break

        username_inp = input('请输入用户名: ')
        pwd_inp = input('请输入密码: ')

        with open('user_info', 'r', encoding='utf-8') as fr:
            for user_info in fr:
                user_info = user_info.strip()
                username, pwd = user_info.split(':')
                if username_inp == username and pwd == pwd_inp:
                    print('登录成功')
                    username_list.append(username_inp)
                    count = 3
                    break

            else:
                print('用户名或密码错误')
                count += 1


def shopping():
    '''购物'''
    print('*' * 20, '购物', '*' * 20)

    while True:

        if not username_list:
            print('请先登录')
            break

        print('''
        1 马云
        2 马化腾
        3 马冬梅
        ''')

        choice = input('请输入你要购买的商品编号,其他任意内容回到主菜单: ')

        if choice not in ['1', '2', '3']:
            break
        # 判断选择商品在购物车中是否存在. 已经存在: 数量加1; 不存在: 数量为1
        goods_name = goods_dict[choice]
        if goods_name in shopping_cart_dict:
            shopping_cart_dict[goods_name] += 1
        else:
            shopping_cart_dict[goods_name] = 1

        print(f'已添加至购物车, 当前购物车商品为:
{goods_dict}')


def shopping_cart():
    '''购物车'''

    if not username_list:
        print('请先登录')
        return

    print('*' * 20, '购物车', '*' * 20)

    if shopping_cart_dict == {}:
        print('购物车为空,请先挑选商品')
        return

    print(f'当前购物车商品有:{shopping_cart_dict}')

    buy_choice = input('确认购买请输入Y,其他任意内容回到主菜单: ')
    if buy_choice not in ['Y', 'y']:
        func()
    shopping_cart_dict.clear()
    print('购买成功!')


def logout():
    '''退出登录'''

    if not username_list:
        print('请先登录')
        return

    choice = input('确认退出登录请输入Y,其他任意内容回到主菜单: ')
    if choice not in ['Y', 'y']:
        return
    username_list.clear()
    print('已退出登录')


func_dict = {
    '1': register,
    '2': login,
    '3': shopping,
    '4': shopping_cart,
    '5': logout
}


def func():
    '''功能菜单'''

    while True:
        print('''
             1 : 注册
             2 : 登录
             3 : 购物
             4 : 购物车
             5 : 退出登录
            ''')

        choice = input('请输入编号选择功能,其他任意内容退出程序: ')
        if choice not in func_dict:
            break
        func_dict[choice]()  # 使用函数对象调用函数


func()

原文地址:https://www.cnblogs.com/bigb/p/11566829.html