购物车程序示例,支持显示购买商品数量

本文提供了一个简单的购物车程序示例,使用Python实现。程序允许用户输入薪水,并从商品列表中选择购买的商品,同时显示剩余金额及已购商品详情。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

goods_list = [("IphoneX", 9000), ("Tesla", 700000), ("Python book", 55),
      ("coffee", 35), ("Computer", 6999), ("Bitcoin", 67000), ("Food", 20), ("TV", 5000)]
shopping_car = []
cost = 0.0


print("Welcome to the shopping market,here is your shopping car.")
print("You can enter 'q' to quit at anytime.")
print("You can enter 'p' to purchase the goods you buy.")
print("You can enter 's' to show your shopping car.")
print("You can enter 'r' to show the goods list again.")
print("Here are the goods list:")
for i, v in enumerate(goods_list, 1):
  print(i, ">>>>>>>>>>>>>>>>", v)

saving = input("Please input your salary: ")
if saving.isdigit():
  saving = float(saving)
  while True:
    choose = input("Please enter the goods NO. you want to buy: ")
    if choose.isdigit():
      choose = int(choose)

      if choose > 0 and choose <= len(goods_list):

        if goods_list[choose - 1][1] < saving:
          shopping_car.append(goods_list[choose - 1])
          cost += goods_list[choose - 1][1]
          saving -= cost
          print("Adding %s in your shopping car,it costs %f CNY" %
            (goods_list[choose - 1][0], goods_list[choose - 1][1]))
        else:
          print("Not enough money to buy this goods.")
      else:
        print("Invalid choosen range.")
    elif choose == "q":
      print("Thanks for coming! See you next time!")
      break
    elif choose == "purchase":
      print("Here is your shopping car".center(70, "-"))
      shopping_car_set = set(shopping_car)
      for goods in shopping_car_set:
        print(goods, "-------------NO:", str(shopping_car.count(goods)),
          "----------COSTS:", goods[1] * shopping_car.count(goods))
      print("Total cost: %f CNY" % cost)
      print("Your saving is now %f CNY" % saving)
      break
    elif choose == "r":
      for i, v in enumerate(goods_list, 1):
        print(i, ">>>>>>>>>>>>>>>>", v)
    elif choose == "s":
      for i in shopping_car:
        print(i)
    else:
      print("Invalid Input.Try again :-)")


 

转载于:https://www.cnblogs.com/fengbo1113/p/7905357.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值