zoukankan      html  css  js  c++  java
  • python作业之购物车

    实现一个购物车的程序

    1、统计每个物品的单价和总价和数量

    2、判断钱是否够

    shop_house = ["小米手机:2001", "苹果手机:5330", "三星手机:4103", "华为手机:3125", "魅族手机:3973", "联想手机:222", "酷派手机:155", "退出", "买单"]
    wel = "welcome to Cevin phone shopping house"
    total_money = 120000
    shop_car_dict = {}
    iphone_num = 0
    xiaomi_num = 0
    sanxing_num = 0
    huawei_num = 0
    meizu_num = 0
    lianxiang_num = 0
    kupai_num = 0
    
    while True:
        print(wel.center(80, '-'))
        for i, phone in enumerate(shop_house):
            print(i, phone)
        option = input("which phone do you want to buy?")
        if option == "0":
            per = shop_house[0].split(":")
            if total_money >= int(per[1]):
                total_money = total_money - int(per[1])
                # print('-'.center("50"))
                print("%s is in shop_car,and you money is %d" % (per[0], total_money))
                if shop_car_dict.get(per[0]):
                    xiaomi_num += 1
                    per_dict = {per[0]: {"总价": int(per[1])*xiaomi_num, "数量": xiaomi_num,"单价":int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
                else:
                    xiaomi_num = 1
                    per_dict = {per[0]: {"总价": int(per[1])*xiaomi_num, "数量": xiaomi_num,"单价":int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
            else:
                print("sorry your money is not enough!")
        elif option == "1":
            per = shop_house[1].split(":")
            if total_money >= int(per[1]):
                total_money = total_money - int(per[1])
                # print('-'.center("50"))
                print("%s is in shop_car,and you money is %d" % (per[0], total_money))
                if shop_car_dict.get(per[0]):
                    iphone_num += 1
                    per_dict = {per[0]: {"总价": int(per[1]) * iphone_num, "数量": iphone_num, "单价": int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
                else:
                    iphone_num = 1
                    per_dict = {per[0]: {"总价": int(per[1]) * iphone_num, "数量": iphone_num, "单价": int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
            else:
                print("sorry your money is not enough!")
        elif option == "2":
            per = shop_house[2].split(":")
            if total_money >= int(per[1]):
                total_money = total_money - int(per[1])
                # print('-'.center("50"))
                print("%s is in shop_car,and you money is %d" % (per[0], total_money))
                if shop_car_dict.get(per[0]):
                    sanxing_num += 1
                    per_dict = {per[0]: {"总价": int(per[1]) * sanxing_num, "数量": sanxing_num, "单价": int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
                else:
                    sanxing_num = 1
                    per_dict = {per[0]: {"总价": int(per[1]) * sanxing_num, "数量": sanxing_num, "单价": int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
            else:
                print("sorry your money is not enough!")
        elif option == "3":
            per = shop_house[3].split(":")
            if total_money >= int(per[1]):
                total_money = total_money - int(per[1])
                # print('-'.center("50"))
                print("%s is in shop_car,and you money is %d" % (per[0], total_money))
                if shop_car_dict.get(per[0]):
                    huawei_num += 1
                    per_dict = {per[0]: {"总价": int(per[1]) * huawei_num, "数量": huawei_num, "单价": int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
                else:
                    huawei_num = 1
                    per_dict = {per[0]: {"总价": int(per[1]) * huawei_num, "数量": huawei_num, "单价": int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
            else:
                print("sorry your money is not enough!")
        elif option == "4":
            per = shop_house[4].split(":")
            if total_money >= int(per[1]):
                total_money = total_money - int(per[1])
                # print('-'.center("50"))
                print("%s is in shop_car,and you money is %d" % (per[0], total_money))
                if shop_car_dict.get(per[0]):
                    meizu_num += 1
                    per_dict = {per[0]: {"总价": int(per[1]) * meizu_num, "数量": meizu_num, "单价": int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
                else:
                    meizu_num = 1
                    per_dict = {per[0]: {"总价": int(per[1]) * meizu_num, "数量": meizu_num, "单价": int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
            else:
                print("sorry your money is not enough!")
        elif option == "5":
            per = shop_house[5].split(":")
            if total_money >= int(per[1]):
                total_money = total_money - int(per[1])
                # print('-'.center("50"))
                print("%s is in shop_car,and you money is %d" % (per[0], total_money))
                if shop_car_dict.get(per[0]):
                    lianxiang_num += 1
                    per_dict = {per[0]: {"总价": int(per[1]) * lianxiang_num, "数量": lianxiang_num, "单价": int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
                else:
                    lianxiang_num = 1
                    per_dict = {per[0]: {"总价": int(per[1]) * lianxiang_num, "数量": lianxiang_num, "单价": int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
            else:
                print("sorry your money is not enough!")
        elif option == "6":
            per = shop_house[6].split(":")
            if total_money >= int(per[1]):
                total_money = total_money - int(per[1])
                # print('-'.center("50"))
                print("%s is in shop_car,and you money is %d" % (per[0], total_money))
                if shop_car_dict.get(per[0]):
                    kupai_num += 1
                    per_dict = {per[0]: {"总价": int(per[1]) * kupai_num, "数量": kupai_num, "单价": int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
                else:
                    kupai_num = 1
                    per_dict = {per[0]: {"总价": int(per[1]) * kupai_num, "数量": kupai_num, "单价": int(per[1])}}
                    shop_car_dict.update(per_dict)
                    print(shop_car_dict)
            else:
                print("sorry your money is not enough!")
        elif option == "7":
            while True:
                option = input("are you sure to exit?
    1:退出
    2:返回")
                if option == "1":
                    print("ByeBye")
                    exit()
                elif option == "2":
                    break
                else:
                    print("input error")
                    continue
    
    
        elif option == "8":
            if shop_car_dict:
                print("you money is %d" % (total_money))
                print(shop_car_dict)
                exit()
            else:
                print("you have buy nothing")
                print("you money is %d" % (total_money))
                exit()
        else:
            print("404")
    

      

  • 相关阅读:
    Discourse 如何不使用 Let’s Encrypt 而使用 CA 签名的密钥进行安装
    Discourse 重复安装过程中的密钥签发问题
    Discourse 升级后提示 https 混合内容
    CentOS 8 安装 docker 报错 containerd.io >= 1.2.2-3
    MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
    培养自己的5项能力
    高效率工作方式
    项目的架构演进过程
    如何预防后台被攻击,且看Tomcat的安全配置
    redis的缓存更新策略,缓存粒度控制
  • 原文地址:https://www.cnblogs.com/bainianminguo/p/6740740.html
Copyright © 2011-2022 走看看