我想添加一个 def 函数,它接受权重并输出成本,但我不知道如何使其与 if elif 循环一起工作
weight = float(input("Enter package weight: ")) #package weight
cost_ground_premium = 125.00 #flat charge for premium
if weight <= 2: #package weighs 2 lb or less
cost_ground = weight * 1.50 + 20 # $1.50/lb, flat charge of $20
elif weight <= 6: #over 2lb but less/equal to 6lb
cost_ground = weight * 3.00 + 20 # $3/lb + flat charge
elif weight <= 10: #over 6lb but less/equal to 10lb
cost_ground = weight * 4.00 + 20 # $4/lb + flat charge
else: #over 10lb
cost_ground = weight * 4.75 + 20 # $4.75/lb + flat charge
print("Ground Shipping $", round(cost_ground, 2))
print("Ground Shipping Premium $", round(cost_ground_premium, 2))
drone_weight = float(input("Enter package weight if using Drone Shipping: "))
if drone_weight <= 2: # $4.50/lb. No flat charge
cost_drone = drone_weight * 4.50 #no flat charge
elif drone_weight <= 6: # $9/lb
cost_drone = drone_weight * 9.00
elif drone_weight <= 10: # $12/lb
cost_drone = drone_weight * 12.00
else: # $14.25/lb
cost_drone = drone_weight * 14.25
print("Drone Shipping $", round(cost_drone, 2))
将整个逻辑包装在 def 中,除了输入之外
def calculateCost():
初始化 重量=1 然后只需使用 while 循环即可。
while weight!=0:
weight = float(input("Enter package weight: "))
calculateCost(weight)
还要确保在函数中,如果权重为 0,则它会通过。
weight = float(input("Enter package weight: ")) #package weight
cost_ground_premium = 125.00 #flat charge for premium
def get_cost_ground(weight, flat_charge=20, is_premimum=False):
# flat charge for premium
if is_premimum:
return cost_ground_premium
if weight <= 2: #package weighs 2 lb or less
cost_ground = weight * 1.50 + flat_charge # $1.50/lb, flat charge of $20
elif weight <= 6: #over 2lb but less/equal to 6lb
cost_ground = weight * 3.00 + flat_charge # $3/lb + flat charge
elif weight <= 10: #over 6lb but less/equal to 10lb
cost_ground = weight * 4.00 + flat_charge # $4/lb + flat charge
else: #over 10lb
cost_ground = weight * 4.75 + flat_charge # $4.75/lb + flat charge
return cost_ground
然后使用
get_cost_ground(weight)
拨打电话。对于高级通话方法get_cost_ground(weight,is_premimum=True)
。
同样,您可以进行无人机运输计算
这是我会采取的(简单)方法,如果您想澄清一些事情,请告诉我:)
SHIPPING_PRICE_DECIMALS = 2
def calculate_ground_shipping_price(weight: float):
FLAT_CHARGE = 20
cost = None
if weight <= 2:
cost = weight * 1.5
elif weight <= 6:
cost = weight * 3
elif weight <= 10:
cost = weight * 4
else:
cost = weight * 4.75
return round(cost + FLAT_CHARGE, SHIPPING_PRICE_DECIMALS)
def calculate_drone_shipping_price(weight):
cost = None
if weight <= 2:
cost = weight * 4.5
elif weight <= 6:
cost = weight * 9
elif weight <= 10:
cost = weight * 12
else:
cost = weight * 14.25
return round(cost, SHIPPING_PRICE_DECIMALS)
weight = 8
ground_shipping_price = calculate_ground_shipping_price(weight)
print(f"ground_shipping_price = {ground_shipping_price}")
drone_shipping_price = calculate_drone_shipping_price(weight)
print(f"drone_shipping_price = {drone_shipping_price}")
如果您希望从同一方法返回两个价格,您可以返回一个像
return (ground_price, drone_price)
这样的元组,然后像 ground_price, second_price = calculate_shipping_price(weight)
那样解压它。
你想定义一个这样的函数:
def calculate_cost(weight: float, costs: dict, flat_fee: float) -> float:
此函数的参数是权重 - 您将通过在其他地方输入获得 - flat_fee,这是所应用的固定费用和成本表,我们将其定义为字典。
然后您的计算函数应该迭代成本表,直到找到有效的重量阈值。
# This is a dict of the costs. The first element is the cost to ship anything greater than the weight of the last key. Keys are the 'weight' and the value is the 'price per pound'.
costs_ground = {
0: 4.75, # This is your 'greater than the rest of the cost structure cost'
2: 1.50,
6: 3.00,
10: 4.0,
}
costs_drone = {
0: 14.25,
2: 4.50,
6: 9.00,
10: 12.00,
}
def calculate_cost(weight: float, costs: dict, flat_fee: float = 0.00) -> float:
indices = sorted(costs)
for idx in indices[1:]: # This skips the first element in the cost structure
if weight <= costs.get(idx, 0.00):
return (costs.get(idx) * weight) + flat_fee)
return (costs.get(0, 0.00) * weight) + flat_fee)
while True: # Will continue until the program is force-killed
weight = float(input("Enter package weight: "))
ground_price = calculate_cost(weight, costs_ground, 20.0)
drone_price = calculate_cost(weight, costs_drone)
print(f"Ground Shipping ${}", round(ground_price, 2))
print(f"Drone Shipping ${}", round(drone_price, 2))
我省略了一些边缘情况,但这向您展示了如何抽象您要定义的函数。
您需要从 if 和 elif 块中声明 cost_ground 和 cost_drone 。 检查下面的代码
def func(weight, drone_weight):
cost_ground = None
cost_drone = None
cost_ground_premium = 125.00 #flat charge for premium
if weight <= 2: #package weighs 2 lb or less
cost_ground = weight * 1.50 + 20 # $1.50/lb, flat charge of $20
elif weight <= 6: #over 2lb but less/equal to 6lb
cost_ground = weight * 3.00 + 20 # $3/lb + flat charge
elif weight <= 10: #over 6lb but less/equal to 10lb
cost_ground = weight * 4.00 + 20 # $4/lb + flat charge
else: #over 10lb
cost_ground = weight * 4.75 + 20 # $4.75/lb + flat charge
if drone_weight <= 2: # $4.50/lb. No flat charge
cost_drone = drone_weight * 4.50 #no flat charge
elif drone_weight <= 6: # $9/lb
cost_drone = drone_weight * 9.00
elif drone_weight <= 10: # $12/lb
cost_drone = drone_weight * 12.00
else: # $14.25/lb
cost_drone = drone_weight * 14.25
return (round(cost_ground, 2), round(cost_drone, 2), cost_ground_premium)
while 1:
weight = float(input("Enter package weight: ")) #package weight
if weight == 0:
break
drone_weight = float(input("Enter package weight if using Drone Shipping: "))
cost_ground, cost_drone, cost_ground_premium = func(weight, drone_weight)
print("Ground Shipping $", cost_ground)
print("Ground Shipping Premium $", cost_ground_premium)
print("Drone Shipping $", cost_drone)