• ~~小练习:python的简易购物车~~


    进击のpython

    *****

    1,用户先给自己的账户充钱:比如先充3000元。
    2,有如下的一个格式:

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

    3,页面显示 序号 + 商品名称 + 商品价格,如:
    1 电脑 1999
    2 鼠标 10

    4,用户输入选择的商品序号,然后打印商品名称及商品价格,并将此商品,添加到购物车(自己定义购物车),用户还可继续添加商品。

    5,如果用户输入的商品序号有误,则提示输入有误,并重新输入。

    6,用户输入N为购物车结算,依次显示用户购物车里面的商品,数量及单价,若充值的钱数不足,则让用户删除某商品,直至可以购买,若充值的钱数充足,则可以直接购买。

    7,用户输入Q或者q退出程序。

    8,退出程序之后,依次显示用户购买的商品,数量,单价,以及此次共消费多少钱,账户余额多少,并将购买信息显示。


    麻烦算法:

    goods = [{"name": "电脑", "price": 1999},
             {"name": "鼠标", "price": 10},
             {"name": "游艇", "price": 20},
             {"name": "美女", "price": 998}, ]
    shop_car = {}
    while 1:
        try:
            m = int(input("请输入充钱金额>>>"))
            count = 0
            for i in goods:
                count += 1
                print(count, i["name"], i["price"])
            while 1:
                cho_goods = input("买点东西啊小老弟>>>")
                try:
                    cho_goods = int(cho_goods)
                    a = (goods[cho_goods - 1]["name"], goods[cho_goods - 1]["price"])
                    if a in shop_car:
                        shop_car[a] += 1
                    else:
                        shop_car[a] = 1
                except ValueError:
                    if cho_goods.upper() == "N":
                        while 1:
                            pay_money = 0
                            for i in range(len(shop_car)):
                                name = list(shop_car.keys())[i][0]
                                price = list(shop_car.keys())[i][1]
                                twice = list(shop_car.values())[i]
                                pay_money += price * twice
                                print(f'商品名称:{name}  商品单价:{price}  购买数量:{twice}')
                            if pay_money > m:
                                msg = input("超额了熬!!删点什么吧")
                                shop_car1 = shop_car.copy()
                                for k in shop_car1:
                                    if msg == k[0]:
                                        shop_car[k] -= 1
                                        if shop_car[k] == 0:
                                            del shop_car[k]
                            else:
                                pay_money = 0
                                print("—————————购买成功—————————")
                                for i in range(len(shop_car)):
                                    name = list(shop_car.keys())[i][0]
                                    price = list(shop_car.keys())[i][1]
                                    twice = list(shop_car.values())[i]
                                    pay_money += price * twice
                                    print(f'商品名称:{name}  商品单价:{price}  购买数量:{twice}')
                                print(f"总共花费:{pay_money}")
                                print(f"钱包余额:{m-pay_money}")
                                print("————————欢迎下次光临————————")
                                exit()
                    elif cho_goods.upper() == "Q":
                        print("欢迎下次光临!")
                        exit()
                    else:
                        print("请重新输入!")
                except IndexError:
                    print("请重新输入")
        except ValueError:
            print("你这钱也不是个正经钱啊!")
    

    简单算法:

    goods = [{"name": "电脑", "price": 1999},
             {"name": "鼠标", "price": 10},
             {"name": "游艇", "price": 20},
             {"name": "美女", "price": 998}, ]
    while 1:
        money = input("充点钱啊!老铁!(按Q退出)")
        try:
            money = int(money)
            name, price, twice = [], [], []
            for i in range(len(goods)):
                print(i + 1, goods[i]["name"], goods[i]["price"])
                name.append(goods[i]["name"])
                price.append(goods[i]["price"])
                twice.append(0)
            while 1:
                num = input("请输入你要购买的东西:")
                try:
                    num = int(num)
                    twice[num - 1] += 1
                    print("以将{}加入购物车,单价为:{}".format(name[num - 1], price[num - 1]))
                except ValueError:
                    if num.upper() == "N":
                        while 1:
                            pay = 0
                            for i in range(len(goods)):
                                if twice[i] <= 0:
                                    pass
                                else:
                                    print(name[i], price[i], twice[i])
                                    pay += price[i] * twice[i]
                            if pay > money:
                                msg = input("超额了熬!!删点什么吧")
                                if msg.upper() == "Q":
                                    print("欢迎下次光临!")
                                    exit()
                                if msg in name:
                                    twice[name.index(msg)] -= 1
                            else:
                                print("—————————购买成功—————————")
                                for i in range(len(goods)):
                                    if twice[i] == 0:
                                        pass
                                    else:
                                        print(f'商品名称:{name[i]}  商品单价:{price[i]}  购买数量:{twice[i]}')
                                print(f"总共花费:{pay}")
                                print(f"钱包余额:{money-pay}")
                                print("————————欢迎下次光临————————")
                                exit()
                    elif num.upper() == "Q":
                        print("欢迎下次光临!")
                        exit()
                    else:
                        print("请重新输入!")
                except IndexError:
                    print("请重新输入")
        except ValueError:
            if money.upper() == "Q":
                print("欢迎下次光临!")
                exit()
            else:
                print("你这钱也不是个正经钱啊!")
    
    

    *学会了吗*
    *凑弟弟*
  • 相关阅读:
    [转]window.open居中
    WebService实例一
    开发步骤
    ubuntu命令
    ubuntu如何添加软件源
    WebService学习笔记
    android.view.WindowManager$BadTokenException: Unable to add window token null is not for an application
    Dialog的使用
    区分Activity的四种加载模式
    在android 中导入项目后 包出现错误
  • 原文地址:https://www.cnblogs.com/jevious/p/11190299.html
Copyright © 2020-2023  润新知