python--简易购物车实现

目标要求:

1、用户输入购物预算

2、打印商品清单,由用户选择,预算够则购买,不够则提示

3、输入q,退出程序

4、购物结束,显示购买的东西和余额

实现:

1、用列表存储商品及价格信息

2、建立空列表存放购买的商品,每次购买成功,就append

3、if判断是否数字、是否是q,是否输入正确的商品编号,while实现循环,直到用户输入q退出

 1 print("---本程序用于购买预算内商品。---")
 2 print("---用户输入预算,然后选择商品,若预算足够,则买入,加入购物车,否则提示不能买。---" )
 3 print("---按q退出购买程序。---\n")
 4 
 5 def printproduct(list):
 6     for idx,item in enumerate(list):
 7         print(idx+1,":",item)
 8 
 9 product=["IPhone 6500","bike 800","Macbook 12000","watch 3000","coffee 31","book 81"]
10 cart=[]
11 
12 budget=input("请输入您的购物预算或按q退出: ")
13 while budget!='q':
14     if str(budget).isdigit():
15         budget=int(budget)
16         printproduct(product)
17         choiceidx = input("请输入您想买的商品序号: ")
18         while choiceidx!='q':
19             if choiceidx.isdigit():
20                 choiceidx=int(choiceidx)
21                 if choiceidx in range(1,len(product)+1):
22                     if int(product[choiceidx - 1].split(" ")[1]) < budget:
23                         print("商品【", product[choiceidx - 1].split(" ")[0], "】购买成功")
24                         cart.append(product[choiceidx - 1].split(" ")[0])
25                         budget -= int(product[choiceidx - 1].split(" ")[1])
26                     else:
27                         print("预算不足,不能购买")
28                 else:
29                     print("请输入正确的商品编号")
30             else:
31                 print("请输入正确的商品编号")
32             printproduct(product)
33             choiceidx = input("请输入您想买的商品序号: ")
34         else:
35             print("您买了:")
36             print(cart)
37             print("您剩余: ",budget)
38             exit()
39     else:
40         print("输入的预算不是数字,请重新输入。")
41         budget = input("请输入您的购物预算: ")
42 print("您买了:")
43 print(cart)
44 print("您剩余: ",budget)

 

转载于:https://www.cnblogs.com/susenyan/p/7217090.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简易购物车程序,使用文件保存购物车中的商品信息和价格。 ```python import os def load_cart(): cart = {} if os.path.exists("cart.txt"): with open("cart.txt", "r") as f: for line in f: name, price = line.strip().split(",") cart[name] = float(price) return cart def save_cart(cart): with open("cart.txt", "w") as f: for name, price in cart.items(): f.write(f"{name},{price}\n") def add_item(cart): name = input("请输入商品名称:") price = input("请输入商品价格:") cart[name] = float(price) print(f"{name}已加入购物车!") def remove_item(cart): name = input("请输入要删除的商品名称:") if name in cart: del cart[name] print(f"{name}已从购物车中删除!") else: print(f"{name}不在购物车中!") def show_cart(cart): print("购物车中的商品:") total_price = 0 for name, price in cart.items(): print(f"{name}:{price}") total_price += price print(f"总价格:{total_price}") def main(): cart = load_cart() while True: choice = input("请选择操作(1.添加商品 2.删除商品 3.查看购物车 4.退出):") if choice == "1": add_item(cart) elif choice == "2": remove_item(cart) elif choice == "3": show_cart(cart) elif choice == "4": save_cart(cart) print("已退出程序。") break else: print("无效的选择,请重新输入。") if __name__ == "__main__": main() ``` 程序中使用了四个函数: - `load_cart`:从文件中加载购物车信息,返回一个字典。 - `save_cart`:将购物车信息保存到文件中。 - `add_item`:向购物车中添加商品。 - `remove_item`:从购物车中删除商品。 - `show_cart`:显示购物车中的商品信息和总价格。 程序的主函数 `main` 中使用一个无限循环,根据用户的选择执行相应的操作。用户选择退出时,程序会调用 `save_cart` 函数将购物车信息保存到文件中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值