python-购物车

作业需求:

  1. 用户先给自己的账户充钱:比如先充3000元。
  2. 页面显示 序号 + 商品名称 + 商品价格,如:
    1 电脑 1999
    2 鼠标 10

    n 购物车结算
  3. 用户输入选择的商品序号,然后打印商品名称及商品价格,并将此商品,添加到购物车,用户还可继续添加商品。
  4. 如果用户输入的商品序号有误,则提示输入有误,并重新输入。
  5. 用户输入n为购物车结算,依次显示用户购物车里面的商品,数量及单价,若充值的钱数不足,则让用户删除某商品,直至可以购买,若充值的钱数充足,则可以直接购买。
  6. 用户输入Q或者q退出程序。
  7. 退出程序之后,依次显示用户购买的商品,数量,单价,以及此次共消费多少钱,账户余额多少。
# auth_Mouni
#date 2019-01-09

product_lst = [
    {"name": " 电脑 ", "price": 1999},
    {"name": " 鼠标 ", "price": 10},
    {"name": " 游艇 ", "price": 20},
    {"name": " 美女 ", "price": 998}]
shop_car = []
cost_money = 0
ID_lst = []  # 移除商品时 所用的列表
print("欢迎光临幸福超市".center(15, "*"))
while 1:
    salary = input("输入金额进行购物:>>")
    if salary.isdigit():
        salary = int(salary)
        break
    else:
        print("输入金额有误,请重新输入")

chajia = 0
while 1:
    print("买商品信息如下".center(20, "*"), "\nID", "商品名称".center(9, " "), "价格")
    for i in range(len(product_lst)):
        print(i, product_lst[i]["name"].center(11, " "), product_lst[i]["price"])  # 输出商品列表信息
        # print(i + 1, product_lst[i]["name"].center(11, " "), product_lst[i]["price"])  # 输出商品列表信息

    choise = input("输入ID将商品加入购物车,[N]结算 [Q]退出:>>")
    if choise.isdigit():  # 开始购物
        index = int(choise)  # 购物车的 ID 下标
        if int(index) < 0 or int(index) >= len(product_lst):
            print("选择有误,重新选择:>>")
            continue
        else:
            for item in shop_car:
                if item["ID"] == index:
                    item["totle"] += 1
                    break
            else:  # 第一次商品加入购物车 增加ID项  和 数量项
                shop_car.append(
                    {"ID": index, 'name': product_lst[index]['name'], "price": product_lst[index]['price'], "totle": 1})
                ID_lst.append(int(index))

    elif choise.upper() == "Q":  # 退出时 不计算商品
        print("退出购物商城,无购物".center(15, "*"))
        man()
    elif choise == "n":  # 结算 商品
        while 1:
            for i in range(len(shop_car)):  # 打印购物车内商品信息
                cost_money += shop_car[i]["price"] * shop_car[i]["totle"]
            while 1:  # 进入移除商品循环
                if len(shop_car) == 0:
                    print("购物车已清空", "\n无消费,余%s元" % (int(salary) - int(cost_money)))
                    exit()
                if int(salary) > int(cost_money):  # 购物金额充足时
                    print("您购买商品信息如下".center(20, "*"), "\n商品名称", "数量".center(10, " "), "价格")
                    for i in range(len(shop_car)):
                        print(shop_car[i]["name"], str(shop_car[i]["totle"]).center(12, " "), shop_car[i]["price"])
                    print("共消费%s元,余%s元" % (int(cost_money), int(salary) - int(cost_money)))
                    exit()
                else:  # 购物金额不足时
                    chajia = int(salary) - int(cost_money)  # 差的钱数

                    print("购物金额不足,请移除部分商品,还差%s元" % (chajia))
                    print("ID", "商品名称".center(5, " "), "数量".center(7, " "), "价格")
                    for i in range(len(shop_car)):  # 打印购物车内要移除的商品列表
                        print(shop_car[i]["ID"], shop_car[i]["name"].center(7, " "),
                              str(shop_car[i]["totle"]).center(9, " "), shop_car[i]["price"])  # 输出商品列表信息
                    while 1:
                        choise = input("输入移除商品ID,退出商城请按[Q]:>>")  # 选择移除商品 ID
                        if choise.isdigit():
                            choise = int(choise)
                            # if choise >= 0 and choise < len(product_lst):  # 存在一个小BUG
                            if choise >= 0 and choise in ID_lst:  # 存在一个小BUG
                                break
                        elif choise.upper() == "Q":
                            print("退出购物车".center(15, "*"))
                        else:
                            print("输入ID有误,请重新输入")
                    for item in shop_car:
                        if item["ID"] == choise and item["totle"] == 1:
                            cost_money -= item["price"]  # 商品为1件时移除商品 花费钱还回来
                            shop_car.remove(item)
                            ID_lst.remove(int(item["ID"]))
                        elif item["ID"] == choise:  # 商品数量 减少
                            cost_money -= item["price"]
                            item["totle"] -= 1
                            ID_lst.remove(int(item["ID"]))  # 删除ID列表的ID号
        exit()
    else:
        print("输入有误,请重新输入:>>")
product_lst = [
    {"name": " 电脑 ", "price": 1999,"ID":0},
    {"name": " 鼠标 ", "price": 10,"ID":1},
    {"name": " 游艇 ", "price": 20,"ID":2},
    {"name": " 美女 ", "price": 998,"ID":3}]
shop_car = []
cost_money = 0
ID_lst = []
print("欢迎光临幸福超市".center(15,"*"))
while 1:
    salary = input("输入金额进行购物:>>")
    if salary.isdigit():
        salary = int(salary)
        break
    else:
        print("输入金额有误,请重新输入")

chajia = 0
while 1:
    print("买商品信息如下".center(20, "*"), "\nID", "商品名称".center(9, " "), "价格")
    for i in range(len(product_lst)):
        print(i, product_lst[i]["name"].center(11, " "), product_lst[i]["price"])  # 输出商品列表信息
        # print(i + 1, product_lst[i]["name"].center(11, " "), product_lst[i]["price"])  # 输出商品列表信息

    choise = input("输入ID将商品加入购物车,[N]结算 [Q]退出:>>")
    if choise.isdigit():  # 开始购物
        index = int(choise)  # 购物车的 ID 下标
        if int(index) < 0 or int(index) >= len(product_lst):
            print("选择有误,重新选择:>>")
            continue
        else:
            for item in shop_car:
                if item["ID"] == index:
                    item["totle"] += 1
                    ID_lst.append(product_lst[index]["ID"])
                    break
            else:  # 第一次商品加入购物车 增加ID项  和 数量项
                shop_car.append({"ID": index, 'name': product_lst[index]['name'], "price": product_lst[index]['price'],"totle": 1})
                ID_lst.append(product_lst[index]["ID"])
    elif choise.upper() == "Q":  # 退出时 不计算商品
        print("退出购物商城,无购物".center(15, "*"))
        
    elif choise == "n":  # 结算 商品
        while 1:
            for i in range(len(shop_car)):  # 打印购物车内商品信息
                cost_money += shop_car[i]["price"] * shop_car[i]["totle"]
            while 1:  # 进入移除商品循环
                if len(shop_car) == 0:
                    print("购物车内无商品")
                    print("购物车已清空","\n无消费,余%s元" % (int(salary) - int(cost_money)))
                    # man()
                if int(salary) > int(cost_money):  # 购物金额充足时
                    print("您购买商品信息如下".center(20, "*"), "\n商品名称", "数量".center(10, " "), "价格")
                    for i in range(len(shop_car)):
                        print(shop_car[i]["name"], str(shop_car[i]["totle"]).center(12, " "), shop_car[i]["price"])
                    print("共消费%s元,余%s元" % (int(cost_money), int(salary) - int(cost_money)))
                    exit()
                else:  # 购物金额不足时
                    chajia = int(salary) - int(cost_money)  # 差的钱数

                    print("购物金额不足,请移除部分商品,还差%s元" % (chajia))
                    print("ID", "商品名称".center(5, " "), "数量".center(7, " "), "价格")
                    for i in range(len(shop_car)):  # 打印购物车内要移除的商品列表
                        print(shop_car[i]["ID"], shop_car[i]["name"].center(7, " "),
                              str(shop_car[i]["totle"]).center(9, " "), shop_car[i]["price"])  # 输出商品列表信息
                    while 1:
                        choise = input("输入移除商品ID,退出商城请按[Q]:>>")  # 选择移除商品 ID
                        if choise.isdigit():
                            choise = int(choise)
                            if choise >= 0 and choise in ID_lst:  # 存在一个小BUG
                                break
                            else:
                                print("输入ID有误,请重新输入")
                                continue
                        elif choise.upper() == "Q":
                            # man()
                        else:
                            print("输入ID有误,请重新输入")
                    for item in shop_car:
                        if item["ID"] == choise and item["totle"] == 1:
                            cost_money -= item["price"]  # 商品为1件时移除 花费钱还回来
                            shop_car.remove(item)
                            ID_lst.remove(int(item["ID"]))

                        elif item["ID"] == choise:  # 商品数量 减少
                            cost_money -= item["price"]
                            item["totle"] -= 1
                            ID_lst.remove(int(item["ID"]))

        exit()
    else:
        print("输入有误,请重新输入:>>")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值