达内Python作业:写出重构购物车代码

达内Python作业:写出重构购物车代码
goods_list = {
    "1": {"name": "哈弗H2", "price": "90000"},
    "2": {"name": "哈弗H6", "price": "103500"},
    "3": {"name": "长安CS75", "price": "100000"},
    "4": {"name": "长安CS95", "price": "170000"}
}
order_list = []


def gouwu():
    while True:
        action = input('请输入(1:购买,2:结算)')
        if action == '1':
            goumai()
        else:
            jiesuan()


def goumai():
    for k, v in goods_list.items():
        print(str(k) + ":" + v['name'] + "," + v['price'] + "元")
    while True:
        goods = input('请输入商品编号:')
        if set(goods_list[goods]):
            break
        else:
            print('商品不存在')
    order_list.append(goods)


def jiesuan():
    total_money = 0
    print('购物车:')
    for i in order_list:
        total_money += float(goods_list[i]['price'])
        print('\t' + (goods_list[i]['name'] + ',价格:' + str(goods_list[i]['price'])))
    print('总价:' + str(total_money))

    while True:
        money = float(input('请输入金额:'))
        if money < total_money:
            print('钱不够,重给!')
        else:
            print('找回' + str(money - total_money))
            order_list.clear()
            break


gouwu()

image.png


最后编辑于:2021/08/28作者: 牛逼PHP

发表评论