购物车实例

# Author Cathy
commodity_list = [[0 ,"iphone",5388],[1 ,"computer",6388],[2 ,"book",20],[3 ,"cake",150],[4 ,"news",10000]]
for i in commodity_list:
    print(i)
your_commodity_list = []#购物车列表
price_list = []#商品价格单独列表
for i in range(4):
    price_list.append(commodity_list[i][2])
min_price_list = min(price_list)#商品中最便宜的价格
not_false = True
salary = input("请输入你的工资:")# 工资
if salary.isdigit():
    salary = int(salary)
    while not_false:
        if salary < min_price_list:
            print("继续努力工作吧!你什么都买不起!~~")
            not_false = False
        else:
            while not_false:
                shopping = input("请输入你要买的商品编号:")
                if shopping.isdigit():
                    shopping = int(shopping)
                    if salary < commodity_list[shopping][2]:#判断编号商品是否能买得起
                        print("你买不起这个啊!~~")
                    else:
                        your_commodity_list.append(commodity_list[shopping])#已购买商品的list
                        print("您购买的商品:",your_commodity_list)
                        Surplus_salary = salary - commodity_list[shopping][2]  # 还剩多少钱
                        print("您还剩余这点钱了:", Surplus_salary)
                        salary = Surplus_salary
                        Continue_to_buy = input("确认是否继续购买?Y/N:")
                        if Continue_to_buy == 'Y' or Continue_to_buy == 'y':
                            break
                        else:
                            print("您购买的商品:", your_commodity_list)
                            print("您还剩余这点钱了:", Surplus_salary)
                            not_false = False
                elif shopping == 'q':
                    print('end shopping')
                    not_false = False
                else:
                    print('invalid input')
                    not_false = False
else:
    print('invalid input')
    not_false = False
View Code

需求:

1. 启动程序,显示商品列表,输入工资

2.输入要购买的商品

3.输入商品后,检测余额是否够用,够就打印已购买的列表,不够就提醒

4.可随时退出购买,退出时,打印已经购买的商品和余额

posted @ 2018-03-16 16:38  Cathy_123  阅读(155)  评论(0编辑  收藏  举报