【问题标题】:unorderable types: NoneType() < NoneType()不可排序的类型:NoneType() < NoneType()
【发布时间】:2019-04-14 06:08:20
【问题描述】:

每当我在我的最便宜的_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 之前有效。

【问题讨论】:

  • 您在末尾缺少return cost,并且您的代码缩进错误。

标签: python


【解决方案1】:

在最后一个 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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 2018-06-05
    • 2019-08-22
    相关资源
    最近更新 更多