python基础--购物车程序

  购物车例子,实现显示商品信息,输入商品编号并且可以减去自己的存入余额,当商品价格大于自己的余额的时候,直接退出;当不再选择商品的时候,退出显示余额和已经添加的商品。

 

#购物车程序

product_list = [
    ("airplane",90000),
    ("pen", 80),
    ("Trek bike", 5000),
    ("Book", 200),
    ("salt", 10),
    ("clothes", 600),

]

saving = input("please input your money:")  #存入的本金
shopping_car = []

if saving.isdigit():      #判断存入的是不是数字
    saving = int(saving)
    while True:
        for i,v in enumerate(product_list,1):
            print(i,">>>>",v)
        choice = input("选择购买商品编号[退出:q]:")

        if choice.isdigit():
            choice = int(choice)
            if choice > 0 and choice <= len(product_list):
                p_item = product_list[choice - 1]
                if p_item[1] < saving:
                    saving -= p_item[1] #减去添加购物车的钱,还剩下的钱数
                    shopping_car.append(p_item) #购物的信息
                else:
                    print("余额不足,还剩%s,不能购买下面的商品"%saving)
                    print(p_item)
            else:
                print("您输入的编号不存在")
        elif choice == "q":
            print("--------您购买的商品如下--------")
            for i in shopping_car:
                print(i)
            print("您还剩%s元钱"%saving)
            break
        else:
            print("invalid input")

 

 

 

#购物车程序

product_list = [
    ("airplane",90000),
    ("pen", 80),
    ("Trek bike", 5000),
    ("Book", 200),
    ("salt", 10),
    ("clothes", 600),

]

saving = input("please input your money:")  #存入的本金
shopping_car = []

if saving.isdigit():      #判断存入的是不是数字
    saving = int(saving)
    while True:
        for i,v in enumerate(product_list,1):
            print(i,">>>>",v)
        choice = input("选择购买商品编号[退出:q]:")

        if choice.isdigit():
            choice = int(choice)
            if choice > 0 and choice <= len(product_list):
                p_item = product_list[choice - 1]
                if p_item[1] < saving:
                    saving -= p_item[1] #减去添加购物车的钱,还剩下的钱数
                    shopping_car.append(p_item) #购物的信息
                else:
                    print("余额不足,还剩%s,不能购买下面的商品"%saving)
                    print(p_item)
            else:
                print("您输入的编号不存在")
        elif choice == "q":
            print("--------您购买的商品如下--------")
            for i in shopping_car:
                print(i)
            print("您还剩%s元钱"%saving)
            break
        else:
            print("invalid input")

 

posted @ 2018-11-04 14:54  Kate_liu  阅读(221)  评论(0编辑  收藏  举报