Week2-购物车程序

#!/usr/bin/en python
# Author:lijun

'''
程序:购物车程序

需求:

启动程序后,让用户输入工资,然后打印商品列表
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
可随时退出,退出时,打印已购买商品和余额
'''

goods_list = [["iphone",6000],["macbook",12000],["book",88],["bike",900]]
salary = input("请输入您的月工资:")

shopping_car=[]
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(goods_list):
            print("商品编号:%d,商品名称:%s,商品价格:%d" %(index,item[0],item[1]))

        want_buy = input("请输入您要购买的商品编号:")

        if want_buy.isdigit():
            want_buy = int(want_buy)
            if want_buy < len(goods_list) and want_buy>=0:
                price=goods_list[want_buy][1]
                if price <= salary:
                    shopping_car.append(goods_list[want_buy])
                    salary-=price
                    print("您购买了商品:%s,余额:%s" %(goods_list[want_buy][0],salary))
                else:
                    print("您的余额为%s,买不起%s" %(salary,goods_list[want_buy][0]))
            else:
                print("输入错误,请输入正确的商品编号和商品名称")
        elif want_buy == "q":
            if shopping_car == []:
                print("退出成功,您的购物车为空,您的余额为:%d" %salary)
            else:
                print("您购买了以下商品:")
                for i in shopping_car:
                    print(i[0])
                print("您的余额为:%s" %salary)
            break
        else:
            print("输入错误.")

 

posted @ 2018-08-28 15:47  领导来视察  阅读(144)  评论(0编辑  收藏  举报