首页 > 编程语言 > 详细

python----作业0329

时间:2017-03-29 15:35:01      阅读:146      评论:0      收藏:0      [点我收藏+]

购物车程序(老师方法)

product_list = [[‘Iphone7‘,5800],                #列表方式
                [‘Coffee‘,30],
                [‘疙瘩汤‘,10],
                [‘Python Book‘,99],
                [‘Bike‘,199],
                [‘ViVo X9‘,2499],

                ]

shopping_cart = []                 #shopping_cart        购物车

salary = int(input("input your salary:"))               #请输入你的资产
while True:
    index = 0
    for product in product_list:
        print(index,product)
        index += 1
    choice = input(">>:").strip()
    if choice.isdigit():   #判断是否为数字
        choice = int(choice)
        if choice >= 0 and choice < len(product_list):    #商品存在
            product = product_list[choice]    #取到商品
            if product[1] <= salary:    #判断是否买得起
                #买得起
                shopping_cart.append(product)   #加入购物车
                salary -= product[1]  #扣钱
                print("Added product" + product[0] + "into shopping cart,your current balance")
            else:
                print("买不起,穷逼!产品价格是" + str(product[1]) + "你还差" + str(product[1] - salary) + "¥")
        else:
            print("商品不存在!")
    elif choice == "q":
        print("------已购买商品列表------")
        for i in shopping_cart:
            print(i)
            print("您的余额为:",salary)
            print("-----End-----")
            break
        else:
            print("无此选项!")

  

python----作业0329

原文:http://www.cnblogs.com/wangyongsong/p/6639928.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!