编辑代码

jd_prices = {
    "北京": {
        "0_10kg": 80,
        "10kg_up_per_kg": 5
    },
    "上海": {
        "0_10kg": 90,
        "10kg_up_per_kg": 6
    },
    "广州": {
        "0_10kg": 85,
        "10kg_up_per_kg": 5.5
    }
}

yuantong_prices = {
    "北京": {
        "0_1kg": 11,
        "1_2kg": 13,
        "2_3kg": 15,
        "3kg_up_base": 17,
        "3kg_up_per_kg": 1.8
    },
    "上海": {
        "0_1kg": 13,
        "1_2kg": 15,
        "2_3kg": 17,
        "3kg_up_base": 19,
        "3kg_up_per_kg": 2.3
    },
    "广州": {
        "0_1kg": 10,
        "1_2kg": 12,
        "2_3kg": 14,
        "3kg_up_base": 16,
        "3kg_up_per_kg": 2.4
    }
}


def calculate_express_fee(company_prices, destination, weight):
    if destination not in company_prices:
        print("暂不支持该目的地的费用计算")
        return None
    price_dict = company_prices[destination]
    if company_prices == jd_prices:
        if weight <= 10:
            fee = price_dict["0_10kg"]
        else:
            fee = price_dict["0_10kg"]+(weight - 10)*price_dict["10kg_up_per_kg"]
    else:
        if weight <= 1:
            fee = price_dict["0_1kg"]
        elif 1 < weight <= 2:
            fee = price_dict["1_2kg"]
        elif 2 < weight <= 3:
            fee = price_dict["2_3kg"]
        else:
            fee = price_dict["3kg_up_base"]+(weight - 3)*price_dict["3kg_up_per_kg"]
    return fee


while True:
    destination = input("请输入目的地(目前支持北京、上海、广州,输入其他将提示不支持):")
    try:
        weight = float(input("请输入包裹重量(kg):"))
        jd_fee = calculate_express_fee(jd_prices, destination, weight)
        yuantong_fee = calculate_express_fee(yuantong_prices, destination, weight)
        if jd_fee is not None and yuantong_fee is not None:
            if jd_fee < yuantong_fee:
                print(f"京东的费用更便宜,费用为:{jd_fee} 元")
            elif jd_fee > yuantong_fee:
                print(f"圆通的费用更便宜,费用为:{yuantong_fee} 元")
            else:
                print(f"两家公司费用相同,均为:{jd_fee} 元")
    except ValueError:
        print("请输入有效的数字")

    choice = input("是否继续计算其他快递费用?(y/n):").lower()
    if choice!= 'y':
        break