zoukankan      html  css  js  c++  java
  • Python小代码_3_购物车

    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		number	Price")
    
                num = 1
                #循环遍历购物车里面的商品,购物车存放的是已买商品
                for i in shopping_car:
                    print(i[0] + "		" + str(num) + "		" + str(i[1]))
                print()
                print("balance :", saving)
                print("---------------")
                break
    
            else:
                print("invalid input")

    本博客内容已全部迁移到我的个人网站 木子窗明(www.muzicm.cn),以后都只会在我的个人网站上首发博客,详情请查看我的个人网站。
  • 相关阅读:
    android获取sd卡路径方法
    Log4Net的使用
    asp.net网站发布
    用网站(WebSite而不是WebProject)项目构建ASP.NET MVC网站
    Asp.Net MVC 路由
    面试题:两个栈模拟队列&&两个队列模拟栈
    TextBlob Quick Start
    链表基本操作题
    leetcode341 扁平化嵌套数组
    细说浏览器输入URL后发生了什么
  • 原文地址:https://www.cnblogs.com/chuangming/p/8439007.html
Copyright © 2011-2022 走看看