!/usr/bin/python3

-- coding: utf-8--

"""
@Author : Meet
@Software: PyCharm
@File : 函数.py
"""

shopping_car = {}
money = input('请输入您要存入的钱数:')
if money.isdigit():
money = int(money)
goods = []

with open('a','r',encoding='utf-8')as f:
    name,price = f.readline().split()
    for i in f:
        k,j = i.split()
        dic={name:k,price:j}
        goods.append(dic)

else:
print('请输入数字!')
while 1:

for i in range(len(goods)):
    print(i+1,list(goods[i].values())[0],list(goods[i].values())[1])

chose = input('请输入您要购买的商品(Q/退出|N/结算):')
if chose.isdigit():
    chose = int(chose) - 1
    shop = list(goods[chose].values())[0]
    s_money = list(goods[chose].values())[1]
    if shop not in shopping_car:
        shopping_car[shop] = {'money':s_money,'num':1}
    else:
        shopping_car[shop]['num'] += 1

elif chose.upper() == 'N':

    shop_money = 0
    for i in shopping_car:
        print(i,shopping_car[i]['money'],shopping_car[i]['num'])
        shop_money += int(shopping_car[i]['money']) * shopping_car[i]['num']

    while int(money) < shop_money:
        print('余额不足!')
        del_num = input('请输入您要删除的商品:')
        shopping_car[del_num]['num'] -= 1
        shop_money -= int(shopping_car[del_num]['money'])
        if shopping_car[del_num]['num'] != 0:
            print(del_num,shopping_car[del_num]['money'],shopping_car[del_num]['num'])
        else:
            shopping_car.pop(del_num)

elif chose.upper() == 'Q':
    money -= shop_money
    for i in shopping_car:
        print(i,shopping_car[i]['money'],shopping_car[i]['num'])
    print('共消费了%s,账户余额剩余:%s'%(shop_money,money))

    with open('buy_goods','a',encoding='utf-8')as f:
        for i in shopping_car:
            f.write('%s,%s,%s'%(i, shopping_car[i]['money'], shopping_car[i]['num']))
    break

else:
    print('输入有误,重新输入!')
02-14 00:08