第一个小程序~~~

一,准备

  1. shopping.txt(在当前目录下),内容如下格式

    name price
    电脑 4999
    鼠标 190
    游艇 2000000
    美女 998
    奔驰 400000
    手机 5000
    
  2. 购物记录.txt'(当前目录下),初始内容如下:

    账户余额:0元
    

二, 开始写啦~~~

f = open('购物记录.txt', mode='r', encoding='utf-8')
moeny = int(f.readlines()[-1].split(':')[1].strip('元'))  # 获取余额
f.close()
print('当前账户余额为:%s元' % moeny)
# 获取商品列表
with open('shopping.txt', mode='r', encoding='utf-8') as f:  # 打开文件以获得商品列表
    goods = []  # 创建列表用于保存商品与价格信息
    lst = f.readline().split()  # 获取字典的key
    for el in f:
        dic = {}
        for v in range(len(el.split())):
            if el.split()[v].isdecimal():
                dic[lst[v]] = int(el.split()[v])
            else:
                dic[lst[v]] = el.split()[v]
        goods.append(dic)
shopping = []  # 购物车
shopping_old = []  # 记录每次购物车
sum_shops = 0  # 记录总消费
# 大循环
while 1:
    print('|商 品 列 表:')  # 打印商品列表
    for index_goods in range(len(goods)):  # 打印商品列表
        print('|' + str(index_goods + 1), goods[index_goods]['name'], goods[index_goods]['price'])
    temp = input('请输入商品序号加入购物车(输入c进行充值,n进行结算,输入q退出):')
    # 充值模块
    if temp.upper() == 'C':
        while 1:
            moeny_input = input('请输入充值金额:')  # 充值
            if moeny_input.isdecimal():
                moeny += int(moeny_input)  # 充值后的金额
                print('充值成功!当前余额为:%s元' % moeny)
                break
            else:
                print('别瞎胡闹,快充钱!!!!!')  # #
    # 购买模块
    elif temp.isdecimal() and 0 < int(temp) <= len(goods):
        print(goods[int(temp) - 1]['name'], goods[int(temp) - 1]['price'], '已加入购物车~~')
        if len(shopping) == 0:  # 第一次添加
            dic = {}
            dic['name'] = goods[int(temp) - 1]['name']
            dic['price'] = goods[int(temp) - 1]['price']
            dic['number'] = 1
            shopping.append(dic)
        else:
            for el_shop in range(len(shopping)):
                if shopping[el_shop]['name'] == goods[int(temp) - 1]['name']:  # 商品在列表中则数量加1
                    shopping[el_shop]['number'] += 1
                elif el_shop == len(shopping) - 1:  # 商品不在列表中则新建字典
                    dic = {}
                    dic['name'] = goods[int(temp) - 1]['name']
                    dic['price'] = goods[int(temp) - 1]['price']
                    dic['number'] = 1
                    shopping.append(dic)
    # 结算模块
    elif temp.upper() == 'N':
        if len(shopping) == 0:  # 提示购物车为空
            print('购物车为空!')
        else:
            while 1:
                sum_shop = 0
                if len(shopping) == 0:  # 如果购物车被清空
                    print('购物车已被清空!')
                    break
                print('序号 商品 单价 数量')
                for ea_shop in range(len(shopping)): # 打印购物车列表,并计算总价
                    print(ea_shop + 1, '\t' + shopping[ea_shop]['name'], ' ', shopping[ea_shop]['price'], ' ', shopping[ea_shop]['number'])
                    sum_shop += shopping[ea_shop]['price'] * shopping[ea_shop]['number']
                if sum_shop > moeny:
                    del_shop = input('余额不足,当前差%s元,请输入想要删除的商品序号:' % (sum_shop - moeny))
                    if del_shop.isdecimal() and 0 < int(del_shop) <= len(shopping):
                        shopping[int(del_shop) - 1]['number'] = shopping[int(del_shop) - 1]['number'] - 1
                        if shopping[int(del_shop) - 1]['number'] == 0:  # 商品数量为0时从购物车删除
                            shopping.pop(int(del_shop) - 1)
                    else:
                        print('输入错误!')
                else:
                    print('购买成功!在家等着吧!')
                    moeny -= sum_shop
                    sum_shops += sum_shop
                    if len(shopping_old) == 0:      # 记录本次购物信息
                        shopping_old = shopping.copy()
                    else:
                        for el in shopping:
                            for el2 in range(len(shopping_old)):
                                if el['name'] == shopping_old[el2]['name']:
                                    shopping_old[el2]['number'] = shopping_old[el2]['number'] + el['number']
                                elif el2 == len(shopping_old) - 1:
                                    shopping_old.append(el)
                    shopping.clear() # 记录本次购物信息后清空购物车,方便接下来继续购物.
                    break
    # 退出模块
    elif temp.upper() == 'Q':
        print('本次购物清单:')
        for el_shoped in shopping_old:   # 打印总购物列表
            print(el_shoped['name'] + '  ' + str(el_shoped['number']) + '\t' + str(el_shoped['price']))
        print('此次共消费:%s元' % (sum_shops))
        print('账户余额:%s元' % (moeny))
        with open('购物记录.txt', mode='a', encoding='utf-8') as f:   # 写入购物信息
            f.write('\n商品  数量  单价')
            for el_shoped in shopping_old:
                f.write('\n' + el_shoped['name'] + '  ' + str(el_shoped['number']) + '\t' + str(el_shoped['price']))
            f.write('\n' + '此次共消费:%s元' % (sum_shops))
            f.write('\n' + '账户余额:%s元' % (moeny))
        print('购物结束,退出程序!')
        break
    else:
        print('输入错误!请重新输入!')
posted @ 2019-06-15 09:59  怀心抱素  阅读(255)  评论(0编辑  收藏  举报