购物车程序作业

自己写的,代码很low,以后进步了在慢慢改吧。

作业需求:

数据结构:
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
......
]

功能要求:
基础要求:

1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表

2、允许用户根据商品编号购买商品

3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒

4、可随时退出,退出时,打印已购买商品和余额

5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示


扩展需求:

1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买

2、允许查询之前的消费记录


# 运行程序之前,请先执行一次2-5行代码,然后注释掉好了
with open('user_info.txt', 'w') as f1:
    f1.write('alex|123|0|
')  # 用户名|密码|工资|已买物品
    f1.write('jack|123|0|
')
    f1.write('mark|123|0|
')

# 正式代码
goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]

user_info = {}
with open('user_info.txt', 'r') as f2:
    for line in f2:
        user = line.strip().split('|')
        user_goods = user[3:-1]
        str_user_goods = '|'.join(user_goods)  # 把已购买的商品转成字符串,因为往文件写入只能写字符串
        user_info[user[0]] = {
            'username': user[0],
            'password': user[1],
            'paycheck': user[2],
            'goods': str_user_goods,
        }
whether_login = False  # 用来判断是否已经登陆过并且输入过工资
while True:
    username = input('用户名:').strip()
    password = input('密码:').strip()
    if username in user_info and password == user_info[username]['password']:
        if int(user_info[username]['paycheck']) != 0:
            whether_login = True
            print('您的余额为{},您的购物车商品如下:'.format(user_info[username]['paycheck']))
            print(user_info[username]['goods'])
        if whether_login == False:  # 如果还没有登陆过
            salary = input('请输入工资:').strip()
        else:
            salary = user_info[username]['paycheck']
        if salary.isdigit() and int(salary) > 0:
            salary = int(salary)
            while True:
                print('商品列表:')
                for index, item in enumerate(goods):
                    print(index, item['name'], item['price'])
                goods_code = input('请输入要购买的商品编号,按q退出:').strip()
                if goods_code.isdigit() and int(goods_code) < len(goods):
                    goods_code = int(goods_code)
                    if salary > goods[goods_code]['price']:
                        salary = salary - goods[goods_code]['price']
                        user_info[username]['paycheck'] = str(salary)
                        user_info[username]['goods'] += goods[goods_code]['name'] + ' |'
                        with open('user_info.txt', 'w') as f3:
                            for i in user_info:
                                f3.write(
                                    i + '|' + user_info[i]['password'] + '|' + str(
                                        user_info[i]['paycheck']) + '|' +
                                    user_info[i]['goods'] + '
'
                                )
                        print('33[31m {0}已加入购物车,余额还剩{1} 33[0m'.format(goods[goods_code]['name'], salary))
                    else:
                        print('余额不足')
                elif goods_code.lower() == 'q':
                    alreaed_buy = '''
                    ---------- 您购买的商品如下: ----------
                    {}
                    欢迎下次光临
                    '''.format(user_info[username]['goods'])
                    print(alreaed_buy)
                    exit()
                else:
                    print('超出范围或输入的不是数字类型')
        else:
            print('输入有误,请重新输入,请输入不小于0的整数')
    else:
        print('用户名或密码错误')
        break

原文地址:https://www.cnblogs.com/lshedward/p/9939137.html