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

    需求:购物车程序
    购买这些东西"iphone","coffee","book","cup"
    对应的价格是4200,30,50,300
    买不起,打印差多少钱
    放入购物车,扣钱,同时打印余额
    选择不买了,打印购买的商品列表、价格和余额

    Products=["iphone","coffee","book","cup"]#商品列表
    Price=[4200,30,50,300]                      #产品价格表
    Salary=int(input("input your salary:"))  #输入工资
    count=0                                   #统计买了的商品数量
    Your_Shopping_Cart=[ ]                     #购买了的商品列表
    Your_Price=[]                              #购买了的商品价格列表
    Your_Price_Totel = 0                       #购买了的商品总价
    while True:
        print("序号", "商品", "价格")
        for a in range(0, len(Products)):
            print("%d   %-7s    %s"%(a + 1,Products[a], Price[a]))
        Choice = input(">>:").strip()
        if Choice.isdigit():#判断输入是否为整数
            Choice = int(Choice)
            #选择购买的商品
            if Choice >=1 and Choice <= len(Products):
                if Price[Choice-1]<=  Salary:
                    Your_Shopping_Cart.append(Products[Choice - 1])
                    # 将购买的商品名字追加进购买了的商品列表
                    Your_Price.append(Price[Choice - 1])
                    # 将选择的商品元素的价格追加进购买了的商品价格列表
                    Salary -=  Price[Choice-1]
                    print("你选的商品"+Products[Choice-1]+"已放入购物车,你的余额为:",Salary)
                    #统计出余额剩多少
                else:
                    print("余额不够,商品价格是"+str(Price[Choice-1])+",你还差"+str(Price[Choice-1]-Salary))
            else:
                print("商品不存在!")
        elif Choice == "no":
            print("-------以购买的商品列表--------")
            print("%s    %8s    %8s    %8s    %8s"%("序号", "商品", "数量", "单价", "总价"))
            New_Your_Shopping_Cart=[]                       #
            for a in Your_Shopping_Cart:                   #
                if a not in New_Your_Shopping_Cart:       #
                    New_Your_Shopping_Cart.append(a)      #购物车去重
            New_Your_Price=[]
            for a in Your_Price:
                if a not in New_Your_Price:
                    New_Your_Price.append(a)             #价格表去重
            for a in  New_Your_Shopping_Cart:
                index=0
                print("%-10s    %-13s    %-8s    %-13s    %-12s"%(index+1,a,Your_Shopping_Cart.count(a),New_Your_Price[index],Your_Shopping_Cart.count(a)*New_Your_Price[index]))#打印出购买了的商品和价格
                index+=1
            print("你的余额为:", Salary)
            break
        else:
            print("无此选项!")
    
    
    
    
    
  • 相关阅读:
    6-查看centos中的用户和用户组
    23-python用BeautifulSoup用抓取a标签内所有数据
    22-python爬虫解决gbk乱码问题
    21-py3 发邮件
    20-调用百度AI的文字识别
    6-Ubuntu与Windows不能相互复制
    2018.4.18 Ubuntu 的telnet命令详解
    2018.4.17 java多线程练习二模拟开场仪式进场
    2018.4.16 Java多线程实现龟兔赛跑
    2018.4.15 Mac系统下如何使用StartUml画好需求分析的类图 (同样适用于windows)
  • 原文地址:https://www.cnblogs.com/domestique/p/6748609.html
Copyright © 2011-2022 走看看