首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何定义一个接受权重并输出价格的函数

如何定义一个接受权重并输出价格的函数
EN

Stack Overflow用户
提问于 2021-11-18 19:01:45
回答 5查看 84关注 0票数 0

我想添加一个接受权重和输出成本的def函数,但我不知道如何使用if elif循环

代码语言:javascript
运行
复制
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))
EN

回答 5

Stack Overflow用户

发布于 2021-11-18 19:09:07

将您的整个逻辑封装在def中,但输入除外

代码语言:javascript
运行
复制
def calculateCost():

初始化weight=1,然后使用while循环。

代码语言:javascript
运行
复制
while weight!=0:
  weight = float(input("Enter package weight: "))
  calculateCost(weight)

还要确保在函数中,如果weight为0,它就会通过。

票数 0
EN

Stack Overflow用户

发布于 2021-11-18 19:18:58

您需要将cost_ground和cost_drone从if和elif块中删除。下面我将为你的代码编写函数和循环。

代码语言:javascript
运行
复制
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)
票数 0
EN

Stack Overflow用户

发布于 2021-11-18 19:22:05

代码语言:javascript
运行
复制
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)。类似地,您可以对无人机运输计算执行此操作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70025346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档