python实现购物车功能和结算功能

import math
商品的信息字典
dict_commodity_info = {
   
    101: {
   "name": "屠龙刀", "price": 10000},
    102: {
   "name": "倚天剑", "price": 10000},
    103: {
   "name": "九阴白骨爪", "price": 8000},
    104: {
   "name": "九阳神功", "price": 9000},
    105: {
   "name": "降龙十八掌", "price": 8000},
    106: {
   "name": "乾坤大挪移", "price": 10000}
}
购买物品的列表
list_orders = []

def print_menu():
    print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
    print("1、购买商品")
    print("2、商品结算")
    print("3、退出系统")
    
  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Python 购物系统,可以实现用户注册、登录、浏览商品、添加购物结算等基本功能。 ```python # 定义商品数据 items = { "1": {"name": "iPhone 12", "price": 7499}, "2": {"name": "AirPods Pro", "price": 1999}, "3": {"name": "iPad Pro", "price": 7999}, "4": {"name": "MacBook Pro", "price": 12999}, "5": {"name": "Apple Watch", "price": 3299} } # 定义用户数据 users = {} # 登录函数 def login(): username = input("请输入用户名:") password = input("请输入密码:") if username in users and users[username]["password"] == password: print("欢迎回来,{}!".format(username)) return username else: print("用户名或密码错误,请重新登录!") return None # 注册函数 def register(): username = input("请输入用户名:") if username in users: print("用户名已存在,请重新注册!") return False else: password = input("请输入密码:") users[username] = {"password": password, "shopping_cart": {}} print("注册成功,请登录!") return True # 浏览商品函数 def browse_items(): print("以下是我们的商品列表:") for num, item in items.items(): print("{0}. {1} - {2} 元".format(num, item["name"], item["price"])) # 添加购物函数 def add_to_cart(username): num = input("请输入要购买的商品编号,按回结束:") if num not in items: print("商品编号不存在,请重新选择!") return else: item = items[num] if num in users[username]["shopping_cart"]: users[username]["shopping_cart"][num]["quantity"] += 1 else: users[username]["shopping_cart"][num] = {"name": item["name"], "price": item["price"], "quantity": 1} print("{0} 已添加到购物!".format(item["name"])) # 查看购物函数 def view_cart(username): cart = users[username]["shopping_cart"] if not cart: print("您的购物为空,快去添加一些商品吧!") else: print("您的购物清单如下:") total_price = 0 for num, item in cart.items(): print("- {0} * {1}:{2} 元".format(item["name"], item["quantity"], item["price"] * item["quantity"])) total_price += item["price"] * item["quantity"] print("总价:{0} 元".format(total_price)) # 结算函数 def checkout(username): cart = users[username]["shopping_cart"] if not cart: print("您的购物为空,不能结算!") else: total_price = 0 for num, item in cart.items(): total_price += item["price"] * item["quantity"] print("您的订单总价为 {0} 元,确认要结算吗?".format(total_price)) confirm = input("请输入 y 或 n:") if confirm == "y": print("结算成功,感谢您的购买!") users[username]["shopping_cart"] = {} else: print("取消结算,欢迎继续购物!") # 主程序 print("欢迎来到 Apple Store!") while True: print("=" * 30) print("请选择您的操作:") print("1. 登录") print("2. 注册") print("3. 浏览商品") print("4. 添加购物") print("5. 查看购物") print("6. 结算") print("7. 退出") choice = input("请输入操作编号:") if choice == "1": username = login() elif choice == "2": register() elif choice == "3": browse_items() elif choice == "4": if "username" not in locals(): print("请先登录!") else: add_to_cart(username) elif choice == "5": if "username" not in locals(): print("请先登录!") else: view_cart(username) elif choice == "6": if "username" not in locals(): print("请先登录!") else: checkout(username) elif choice == "7": print("欢迎下次光临!") break else: print("输入有误,请重新选择!") ``` 程序功能说明: 1. 定义了商品数据和用户数据,以及登录、注册、浏览商品、添加购物、查看购物结算等函数; 2. 主程序循环等待用户输入操作编号,根据不同的操作调用相应的函数; 3. 用户登录后,其购物数据将保存在 `users` 字典中。 希望这个示例对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值