簡體   English   中英

如何定義一個帶權重並輸出價格的 function

[英]How to define a function that takes a weight and outputs a price

我想添加一個 def function 來增加重量和輸出成本,但我不知道如何使用 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():

初始化 weight=1 然后只使用一個while循環。

while weight!=0:
  weight = float(input("Enter package weight: "))
  calculateCost(weight)

還要確保在 function 中,如果重量為 0,它就可以通過。

您需要從 if 和 elif 塊中刪除 cost_ground 和 cost_drone。 下面我寫 function 並為您的代碼循環。


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
    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)
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)一樣解壓它。

您想像這樣定義 function :

def calculate_cost(weight: float, costs: dict, flat_fee: float) -> float:

這個 function 的 arguments 是重量 - 您將通過在其他地方輸入 - flat_fee,這是應用的固定費用和成本表,我們將其定義為字典。

然后您的計算 function 應該遍歷成本表,直到找到有效的重量閾值。

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

我省略了一些邊緣情況,但這向您展示了如何抽象您嘗試定義的 function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM