python代码一个运输公司从网上得到订单订单上标有货物重量和运输里程该公司可以使用3种运输工具:卡车、火车、飞机。编写运输接口声明3个接口常量表示运输工具声明一个计算运费的方法参数是重量和里程。提示:
可以使用枚举(Enum)来声明运输工具的常量,然后在计算运费的方法中根据重量和里程来判断使用哪种运输工具,并返回相应的运费。
代码示例:
from enum import Enum
# 声明运输工具的常量
class Transport(Enum):
TRUCK = 1
TRAIN = 2
PLANE = 3
# 计算运费的方法
def calculate_shipping(weight, distance):
if weight <= 500 and distance <= 1000:
return 50
elif weight <= 1000 and distance <= 2000:
if weight <= 500 or distance <= 1000:
return 50
else:
return 100
elif weight > 1000 or distance > 2000:
if weight > 1000 and distance > 2000:
return 300
else:
return 200
# 测试代码
weight = 800
distance = 1500
shipping_cost = calculate_shipping(weight, distance)
print("运输工具:", Transport.TRUCK.name)
print("重量:", weight, "kg")
print("里程:", distance, "km")
print("运费:", shipping_cost, "元")
输出结果:
运输工具: TRUCK
重量: 800 kg
里程: 1500 km
运费: 100 元
根据重量和里程的不同情况,可以调整计算运费的方法中的条件判断和返回值
原文地址: https://www.cveoy.top/t/topic/hIQa 著作权归作者所有。请勿转载和采集!