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")
    

      

  • 相关阅读:
    Oracle 恢复[rman全备份集+当期归档日志]
    将ping结果输出到txt文件
    诗经 硕鼠 注释
    DIV里Table的宽度设置为100%后页面出现滚动条的解决办法;DIV下移的解决办法 IE 和 FireFox 都通过
    2007春节上海南站买火车票实录
    GG和baidu网络广告真的那么好做吗菜鸟不要被人忽悠了。做站长两个月总结
    iframe 自适应高度 IE Firefox 通过
    飘云QQ宣布终止后续开发 称不懂游戏规则玩不起
    我的小站:诗词在线 http://www.chinapoesy.com 欢迎大家测试速度。特别是网通的。
    丑奴儿欣赏 辛弃疾 诗词在线
  • 原文地址:https://www.cnblogs.com/bainianminguo/p/6740740.html
Copyright © 2011-2022 走看看