Burnov

导航

python-day02-购物车

购物车

需求:

  1、启动程序后,让用户输入工资,然后打印商品列表;

  2、容许用户根据商品编号购买商品;

  3、用户选择商品后,检测余额是否足够,够了就直接扣款,不够就提醒客户;

  4、随时可以退出,退出时打印出购买到的商品和余额;

流程图:

代码:

product_list = [
 ('iPhone', 6000),
 ('MAC Por', 9800),
 ('Bike', 800),
 ('iWatch', 2000),
 ('Coffee', 32)                  
]
salary = input("请输入您的工资:")
shopping_list = []

if salary.isdigit():
    salary = int(salary)
    while True:
        for item in product_list:
            print(product_list.index(item), item)
        user_choice = input("请选择您需要的商品:")
        if user_choice.isdigit():
            user_choice = int(user_choice)
        product_item = product_list[user_choice]
            if product_item[1] <= salary:
          shopping_list.append(product_item)
          salary -= product_item[1]
          print("%s已经添加到您的购物车,您的余额为:%s" % (product_item, salary))
        else:
          print("余额不足,请充值")
     elif user_choice == 'q'
         print("您已经购买了如下商品:%s\n余额为:%s\n谢谢惠顾")
        exit()
     else:
         print("无效的选项")

 

posted on 2019-01-06 22:01  Burnov  阅读(197)  评论(0编辑  收藏  举报