无序类型:NoneType()<NoneType()

2024-10-01 09:40:48 发布

您现在位置:Python中文网/ 问答频道 /正文

每当我在cheapest_shipping函数中输入大于10的值时,就会出现无序类型错误。在

我试着用单独的变量传递给函数来计算成本。也尝试了不同的比较运算符。在

cost = 0
pgs = 125

def gsp(weight):
  if weight == 0:
cost = 20
return cost
  elif weight <= 2:
cost = (1.5 * weight + 20)
return cost 
  elif weight > 2 and weight <= 6:
cost = (3 * weight + 20)
return cost
  elif weight > 6 and weight <= 10:
cost = (4 * weight + 20)
return cost
  elif weight > 10:
cost = (4.75 * weight + 20)

def dsp(weight):
  if weight == 0:
cost = 0
return cost
  elif weight <= 2:
cost = (4.5 * weight)
return cost 
  elif weight > 2 and weight <= 6:
cost = (9 * weight)
return cost
  elif weight > 6 and weight <= 10:
cost = (12 * weight)
return cost
  elif weight > 10:
cost = (14.25 * weight)

def cheapest_shipping(weight):
  if gsp(weight) < dsp(weight) and gsp(weight) < pgs:
return "Ground shipping is the cheapest option at $" + str(gsp(weight))
  elif dsp(weight) < gsp(weight) and dsp(weight) < pgs:
return "Drone shipping is the cheapest option at $" + str(dsp(weight))

print (cheapest_shipping(11))

该函数使用我的地面运输价格函数和无人机运输价格函数以及优质地面运输的成本,并取重量输入,应该返回最便宜的运输选项。直到输入超过10。在


Tags: andthe函数returnifisdefshipping
1条回答
网友
1楼 · 发布于 2024-10-01 09:40:48

最后一条elif语句中缺少return cost。这可能是你想要的

cost = 0
pgs = 125


def gsp(weight):
    if weight == 0:
        cost = 20
        return cost
    elif weight <= 2:
        cost = (1.5 * weight + 20)
        return cost
    elif weight > 2 and weight <= 6:
        cost = (3 * weight + 20)
        return cost
    elif weight > 6 and weight <= 10:
        cost = (4 * weight + 20)
        return cost
    elif weight > 10:
        cost = (4.75 * weight + 20)
        return cost


def dsp(weight):
    if weight == 0:
        cost = 0
        return cost
    elif weight <= 2:
        cost = (4.5 * weight)
        return cost
    elif 2 < weight <= 6:
        cost = (9 * weight)
        return cost
    elif 6 < weight <= 10:
        cost = (12 * weight)
        return cost
    elif weight > 10:
        cost = (14.25 * weight)
        return cost


def cheapest_shipping(weight):
    if gsp(weight) < dsp(weight) and gsp(weight) < pgs:
        return "Ground shipping is the cheapest option at $" + str(gsp(weight))
    elif dsp(weight) < gsp(weight) and dsp(weight) < pgs:
        return "Drone shipping is the cheapest option at $" + str(dsp(weight))


print (cheapest_shipping(11))

相关问题 更多 >