python--购物车(简易版)

南笙酒味 提交于 2020-01-02 01:22:16
#简易版购物车#__author:  liangchen     Date:   2019/8/12
things = [
    ('iphone6s',5800),
    ('mac book',9000),
    ('coffee',32),
    ('python boook',80),
    ('bicycle',1500)
]
saving = input("please input your saving:")         #拥有的本金
shopping_car = []
if saving.isdigit():
    saving = int(saving)
    while True:
        #打印商品内容
        for i,v in enumerate(things,1):
            print(i,'---',v)
        thing_num = input("请选择你要购买商品的编号(退出:q):")
        #验证购买的是否是已有商品
        if thing_num.isdigit():
            thing_num = int(thing_num)
            if thing_num > 0 and thing_num <= len(things):
                thing = things[thing_num-1]
                #判断是否有足够余额,如果有,则余额为本金减去该商品的价格并加入购物车
                if thing[1] < saving:
                    saving -= thing[1]
                    shopping_car.append(thing)
                    print(thing,"已加入购物车")
                else:
                    print("余额不足,还剩%s"%saving)
            else:
                print("error Item number")
        elif thing_num == 'q':
            print("-------您的购物车有如下商品--------")
            #循环遍历购物车里的商品
            for i in shopping_car:
                print(i)
            print("您还剩余额 %s元"%saving)
            break
        else:
            print("Not this thing!")
else:
    print("Invalid saving!")

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!