栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实现简单的三乐购物系统

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实现简单的三乐购物系统

主要内容:
  • 实现登录,注册,查看商品信息,加入购物车,查看并结算购物车,退出功能
  • 定义相关变量
  • 主程序的运行流程
代码实现:

功能统一使用函数包装,由主程序调用即可

# 登录验证
def login(username, passwd):
    if username in user  and user[username]["passwd"] == passwd:
        return True
# 注册用户
def register(username, passwd, money):
    if username in user:
        print("用户名已存在!请重新输入!")
    else:
        user[username] = {"passwd": passwd, "money": money}
        print(f"{username}注册成功!")
        print(user)
# 查看商品信息
def message():
    print("商品信息如下".center(50,"-"))
    print(f"{'商品编号':<7}{'商品名称':<8}{'单价':<7}")
    for i in goodslist:
        print(f"{i:<10}{goodslist[i]['name']:<10}{goodslist[i]['price']:<10}")
    print("-"*50)
# 加入购物车
def shopping(shop,shopnum):
    shoplist[shop] = shoplist.get(shop,0) + shopnum
    print("加购成功")
    print(f"{'商品名':<8}{'数量':<7}")
    print(f"{shop:<12}{shopnum:<10}")
# 查看购物车
def check():
    print("购物车".center(40,"-"))
    print(f"{'商品编号':<8}{'商品名':<8}{'单价':<7}{'数量':<7}")
    for i in shoplist:
        print(f"{i:<11}{goodslist[i]['name']:<10}{goodslist[i]['price']:<8}{shoplist[i]:<10}")
    print("-"*40)
# 结算购物车
def pay(pay1):
    global shoplistmoney,shoplistmoney2
    for i in shoplist:
        shoplistmoney += shoplist[i] * goodslist[i]['price']
    if shoplistmoney <= user[username]['money']:
        user[username]['money'] -= shoplistmoney
        print(f"结算成功,本次消费{shoplistmoney},剩余{user[username]['money']}")
        shoplist2.update(shoplist.copy())
        shoplist.clear()
        shoplistmoney2 += shoplistmoney
        shoplistmoney = 0
    else:
        print(f"余额不足,为:{user[username]['money']}")
        shoplist.clear()
def exit():
    global shoplistmoney2
    print("谢谢惠顾!欢迎下次光临!".center(30))
    print("-"*38)
    print(f"{'商品编号':<8}{'商品名称':<8}{'单价':<7}{'数量':<7}")
    for i in shoplist2:
        print(f"{i:<11}{goodslist[i]['name']:<10}{goodslist[i]['price']:<8}{shoplist2[i]:<10}")
    print("-" * 38)
    print(f"总共消费:{shoplistmoney2},余额为:{user[username]['money']}")
    shoplist2.clear()   #退出之后清空购物单
    shoplistmoney2 = 0     #将总消费金额置0

定义相关变量 

# 相关变量
# 用户信息字典
user = {"root": {"passwd":"123456", "money":100}}
# 商品信息字典
goodslist = {'F01': {'name': '苹果', 'price': 10},
             'F02': {'name': '香蕉', 'price': 5.5},
             'F03': {'name': '橙子', 'price': 15.6},
             'F04': {'name': '柚子', 'price': 3.6},
             'F05': {'name': '牛奶', 'price': 45},
             'F06': {'name': '酸奶', 'price': 12}}
shoplist = {}  #购物车清单
shoplist2 = {}  #已购物清单
shoplistmoney = 0 #剩余金额
shoplistmoney2 = 0 #消费金额

程序具体流程

# 主程序
print("欢迎进入三乐购物系统".center(40,"*"))
while 1:
    print("1、登录".center(40))
    print("2、注册".center(40))
    print("3、退出".center(40))
    option = input("请输入你的选择:")
    if option == '1':
        username = input("请输入用户名:")
        passwd = input("请输入密码:")
        if login(username, passwd):
            print("登录成功!")
            print(f"欢迎进入购物系统,您的余额为:{user[username]['money']}")
            while 2:
                print("1,查看商品信息   ".center(52))
                print("2,将商品加入购物车".center(50))
                print("3,查看与结算购物车".center(50))
                print("4,退出          ".center(54))
                option2 = input("请输入您的选择: ")
                if option2 == "1":
                    message()
                elif option2 == "2":
                    shop = input("请输入商品编号(F01~F06): ")
                    if shop in goodslist:
                        shopnum = input("请输入购买数量:")
                        if shopnum.isdigit():
                            shopnum = int(shopnum)
                            shopping(shop,shopnum)
                        else:
                            print("请输入正确的数量")
                    elif shop == "q":
                        continue
                    else:
                        print("商品不存在")
                elif option2 == "3":
                    check()
                    pay1 = input("结算请按5(其他键返回菜单): ")
                    if pay1 == '5':
                        pay(pay1)
                elif option2 == "4":
                    exit()
                    break
                else:
                    print("您的输入有误,请重新输入")
        else:
            print("登录失败!")
    elif option == '2':
        username = input("请输入用户名:")
        passwd = input("请输入密码:")
        money = input("请输入金额:")
        register(username, passwd, money)
    elif option == "3":
        print("欢迎下次光临")
        break
    else:
        print("输入有误")

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1016099.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号