码迷,mamicode.com
首页 > 编程语言 > 详细

Python小代码_3_购物车

时间:2018-02-10 17:01:53      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:代码   int   pos   one   put   you   输入   ima   mon   

product_list = [
    (MacBook, 9000),
    (kindle, 500),
    (tesla, 900000),
    (book, 100),
    (bike, 2000),
]

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("choose goods that you want to buy[exit:q]:")

        #验证输入是否合法
        if choice.isdigit():
            choice = int(choice)
            if choice > 0 and choice <= len(product_list):

                #将用户选择商品通过choice选出
                p_item = product_list[choice - 1]

                #如果钱足够,用saving减去商品价格,并将该商品加入购物车
                if p_item[1] <= saving:
                    saving -= p_item[1]
                    shopping_car.append(p_item)

                else:
                    print("-----------------")
                    print("Sorry, your balance is not enough.")
                    print("Your balance: " + str(saving))
                    print("-----------------")
                    continue
                print("-----------------")
                print("You have chose " + p_item[0])
                print("Your balance: " + str(saving))
                print("-----------------")
            else:
                print("Non existent")
        elif choice == q:
            print("---------------")
            print("You have chose the following goods:")
            print("Goods\t\tnumber\tPrice")

            num = 1
            #循环遍历购物车里面的商品,购物车存放的是已买商品
            for i in shopping_car:
                print(i[0] + "\t\t" + str(num) + "\t\t" + str(i[1]))
            print()
            print("balance :", saving)
            print("---------------")
            break

        else:
            print("invalid input")

技术分享图片

 

Python小代码_3_购物车

标签:代码   int   pos   one   put   you   输入   ima   mon   

原文地址:https://www.cnblogs.com/chuangming/p/8439007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!