用python来编写一个超市购物小系统

本文介绍了一个简单的购物车程序设计,该程序使用Python实现,能够显示商品列表,允许用户根据输入的商品编号购买商品,并实时更新用户的余额。程序还包含了退出选项,以便用户查看已购买的商品和剩余金额。
# author:Iron Qi
product_list = [('Iphone',5800),
                ('Mac Pro',9800),
                ('Play Game',1000),
                ('Watch',4300),
                ('Basketball',260),
                ('cloth',1314)
               ]
shopping_list=[]  #商品购物车是空表
salary = input("Input your salary:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(product_list): # 取列表的下标和实际属性
            print(index,item)
        user_choice = input("Please select the item you want to buy:") # 选择的是商品编号
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(product_list)and user_choice >=0:
                p_price=product_list[user_choice]
                if p_price[1] <= salary: #can buy
                    shopping_list.append(p_price)
                    salary -=p_price[1]
                    print("Added %s into shopping cart,your balance is \033[31;1m%s\033[0m" %(p_price,salary))
                else:
                    print("\033[32;1myour balance only is[%s],No longer available\033[0m"% salary)
            else:
                print("product code [%s] is not exist!"% user_choice)
        elif user_choice =='q':
            print("---------shopping list---------")
            for p in shopping_list:
                print(p)
            print("your current balance:",salary)
            exit()
        else:
            print("invalid option")
else:
    print("\033[33;1mYour input is not valid\033[0m") #\033[33;1m----\033[0m 是用来高亮。

 

那个关键字的高亮是固定格式,31,32,33,41,42,43分别代表字体是绿,红,黄和关键字背景颜色是绿,红,黄。希望博友来踩! 

编写一个简单的Python超市购物程序,可以模拟用户选择商品、添加到购物车、查看购物车以及计算总价的过程。这里是一个基本的示例: ```python class Product: def __init__(self, name, price): self.name = name self.price = price class ShoppingCart: def __init__(self): self.items = {} def add_item(self, product_name, quantity): if product_name in self.items: self.items[product_name] += quantity else: self.items[product_name] = quantity def remove_item(self, product_name, quantity=1): if product_name in self.items and self.items[product_name] >= quantity: self.items[product_name] -= quantity if self.items[product_name] == 0: del self.items[product_name] def total_cost(self): return sum(item['price'] * item['quantity'] for item in self.items.values()) # 示例商品 products = { "苹果": Product("苹果", 5), "香蕉": Product("香蕉", 3), "牛奶": Product("牛奶", 10) } def main(): cart = ShoppingCart() while True: print("\n超市购物助手") print("1. 添加商品") print("2. 查看购物车") print("3. 删除商品") print("4. 结算") print("5. 退出") choice = input("请输入您的选择(1-5): ") if choice == '1': product_name = input("请输入您想购买的商品名称: ") quantity = int(input("请输入购买数量: ")) cart.add_item(product_name, quantity) elif choice == '2': print("当前购物车:") for item, quantity in cart.items.items(): print(f"{item}: {quantity} x {products[item].price}") elif choice == '3': product_name = input("请输入您要删除的商品名称 (按回车键结束): ") if product_name: cart.remove_item(product_name) elif choice == '4': print(f"总费用: {cart.total_cost()} 元") elif choice == '5': break else: print("无效的选择,请重新输入.") if __name__ == "__main__": main() ``` 在这个程序中,我们首先定义了Product类表示商品,然后创建了一个ShoppingCart类作为购物车容器。主函数`main()`处理用户的交互,包括添加商品、查看购物车等操作,并计算总价。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值