python---代码重构函数封装调用

将下面代码重构(命名、优化)

shang_pin_info = {
    101: {"name": "屠龙刀", "price": 10000},
    102: {"name": "倚天剑", "price": 10000},
    103: {"name": "九阴白骨爪", "price": 8000},
    104: {"name": "九阳神功", "price": 9000},
    105: {"name": "降龙十八掌", "price": 8000},
    106: {"name": "乾坤大挪移", "price": 10000}
}

ding_dan = []


def gou_wu():
    """
        购物
    :return:
    """
    while True:
        item = input("1键购买,2键结算。")
        if item == "1":
            for key, value in shang_pin_info.items():
                print("编号:%d,名称:%s,单价:%d。" % (key, value["name"], value["price"]))
            while True:
                cid = int(input("请输入商品编号:"))
                if cid in shang_pin_info:
                    break
                else:
                    print("该商品不存在")
            count = int(input("请输入购买数量:"))
            ding_dan.append({"cid": cid, "count": count})
            print("添加到购物车。")
        elif item == "2":
            zong_jia = 0
            for item in ding_dan:
                shang_pin = shang_pin_info[item["cid"]]
                print("商品:%s,单价:%d,数量:%d." % (shang_pin["name"], shang_pin["price"], item["count"]))
                zong_jia += shang_pin["price"] * item["count"]
            while True:
                qian = float(input("总价%d元,请输入金额:" % zong_jia))
                if qian >= zong_jia:
                    print("购买成功,找回:%d元。" % (qian - zong_jia))
                    ding_dan.clear()
                    break
                else:
                    print("金额不足.")


gou_wu()

重构后

# 数据
dict_commodity_info = {
    101: {"name": "屠龙刀", "price": 10000},
    102: {"name": "倚天剑", "price": 10000},
    103: {"name": "九阴白骨爪", "price": 8000},
    104: {"name": "九阳神功", "price": 9000},
    105: {"name": "降龙十八掌", "price": 8000},
    106: {"name": "乾坤大挪移", "price": 10000}
}

list_order = []

# 对数据的操作
def shopping():
    """
        购物
    :return:
    """
    while True:
        item = input("1键购买,2键结算。")
        if item == "1":
            buying()
        elif item == "2":
            settlement()

def settlement():
    """
        结算
    """
    print_orders_info()
    total_price = calculate_total_price()
    paying(total_price)

def paying(total_price):
    """
        支付
    :param total_price: 需要支付的金额
    """
    while True:
        money = float(input("总价%d元,请输入金额:" % total_price))
        if money >= total_price:
            print("购买成功,找回:%d元。" % (money - total_price))
            list_order.clear()
            break
        else:
            print("金额不足.")

def calculate_total_price():
    """
        计算总价格
    :return: 总价格
    """
    total_price = 0
    for item in list_order:
        commodity = dict_commodity_info[item["cid"]]
        total_price += commodity["price"] * item["count"]
    return total_price

def print_orders_info():
    """
        打印所有订单信息
    """
    for item in list_order:
        commodity = dict_commodity_info[item["cid"]]
        print("商品:%s,单价:%d,数量:%d." % (commodity["name"], commodity["price"], item["count"]))

def buying():
    """
        购买
    """
    print_commodity_info()
    order = create_order()
    list_order.append(order)
    print("添加到购物车。")

def create_order():
    """
        创建订单
    :return: 字典类型的订单
    """
    while True:
        cid = int(input("请输入商品编号:"))
        if cid in dict_commodity_info:
            break
        else:
            print("该商品不存在")
    count = int(input("请输入购买数量:"))
    order = {"cid": cid, "count": count}
    return order

def print_commodity_info():
    """
        打印商品信息
    """
    for key, value in dict_commodity_info.items():
        print("编号:%d,名称:%s,单价:%d。" % (key, value["name"], value["price"]))

shopping()

重构refactor,pycharm有此功能,方便实现重构代码

明确过程,将想要表达的思路展示出来,用函数一层层剥开过程,以函数分解步骤,展示清晰,不需要大篇幅注释,通过看代码及简单注释就可清晰代码含义
提高函数的可重复使用性,与可维护性,代码灵活
选中需要封装的函数体代码(ctrl+alt+m),显示出函数方法名称修改提示框,可修改函数命名,确认后会自动封装好,
改函数名快捷方法:shift+f6,将涉及到的全部函数名字修改完成

你可能感兴趣的:(笔记)