0x1 前言

Python语言能做很多东西的,像数据分析啊、自动化、开发、

爬虫(真的很棒哟,初学者玩很有成就感的啊哈哈)等等还有挺多。

0x2 用Python编写的一个小型购物车程序

import time
money = 0
goods_lst = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
    {"name": "风油精", "price": 30},
]
# 定义购物车字典
car = {}
# 1:{电脑 ... }
# 2:{鼠标 .... }
def recharge():
    global money
    while True:
        num = input("请充值吧,大哥")
        if num.isdecimal():
            money = int(num)
            print("恭喜你~ 充值成功{}元人民币".format(money))
            break
        else:
            print("充值失败,非数字.")
def loading():
    print("加载商品中 ... ")
    time.sleep(1)
def show_goods():
    strvar = "{}".format("商品名称").center(18)
    print("序号" + strvar + "价格")
    for k, v in enumerate(goods_lst, start=1):
        v['num'] = k
        strvar = "{v[num]:<10}{v[name]:<12}{v[price]}".format(v=v)
        print(strvar)
def error():
    strvar = '''
**************************************************
*           您输入的选项不存在 , 请重新输入          *
**************************************************
'''
    print(strvar)
    time.sleep(1)
def add_car(num):
    """如果在购物车中,在数量上加1,如果不在购物车中,就添加新商品"""
    if num not in car:
        car[num] = {"name": goods_lst[num - 1]["name"], "price": goods_lst[num - 1]["price"], "amount": 1}
    else:
        car[num]["amount"] += 1
    print(car)
def show_car(num):
    # print(car)
    print("*" * 50)
    print("您选择的商品具体信息:")
    print("*-商品名称:{}".format(car[num]["name"]))
    print("*-商品单价:{}".format(car[num]["price"]))
    print("*-商品数量:{}".format(car[num]["amount"]))
    print("已成功添加到购物车~ 请继续shopping ~")
    print("*" * 50)
def balance():
    total = 0
    print("[-------------------您购物车的具体商品如下:-------------------]")
    # 序号2: 商品名称:鼠标 商品单价:10 商品数量:1 此商品总价:10
    for k, v in car.items():
        v["num"] = k
        v["total"] = v["price"] * v["amount"]
        total += v["total"]
        strvar = "序号{v[num]}: 商品名称:{v[name]} 商品单价:{v[price]} 商品数量:{v[amount]} 此商品总价:{v[total]}".format(v=v)
        print(strvar)
    return total
# total 一共花的钱,money 充值金额
def success(total, money):
    print("[一共:{}元]".format(total))
    print("[您已经成功购买以上所有商品 , 余额还剩{}元,感谢您下次光临~]".format(money - total))
    time.sleep(1)
def del_goods(total, money):
    print("余额不足,还差{}元,请忍痛割爱,删除某些商品".format(total - money))
    sn = input("[-------------------请输入要删除的商品序号:-------------------]    ")
    if sn.isdecimal():
        sn = int(sn)
        if sn in car:
            car[sn]["amount"] -= 1
            if not car[sn]["amount"]:
                car.pop(sn)
        else:
            error()
    else:
        error()
def myexit():
    print("[============== 欢迎下次光临: ==============]")
    time.sleep(1)
# N结算 Q退出 序号购买
# 主函数,进行统一调用
def main():
    # 充值
    recharge()
    # 加载中
    # loading()
    # 展示商品
    show_goods()
    # 开始购物
    sign = True
    while sign:
        num = input("请输入您要购买的商品")
        if num.isdecimal():
            num = int(num)
            if (1 <= num) and (num <= len(goods_lst)):
                add_car(num)
            else:
                print("输入有误,没有该序号的商品,请重新输入")
                continue
            # 展示购物车
            show_car(num)
        elif num.upper() == "N":
            while True:
                # 开始结算
                total = balance()
                if total > money:
                    # 删除商品
                    del_goods(total, money)
                else:
                    # 购买成功
                    success(total, money)
                    sign = False
                    break
            print(total)
        elif num.upper() == "Q":
            # 退出
            myexit()
            sign = False
        else:
            error()
main()

  

发表回复