首页 > 解决方案 > Codecademy 的 Python 3 模块中 Sal 的 Shipping 问题

问题描述

我是stackoverflow的新手,我最初在这里发表评论将其作为评论发布,因为它是相关的,但建议我将其作为新帖子,所以这里是:

我解决课程的尝试看起来类似于 OP,但粗略的外观给了我一些修复它的想法,我想在此处摆姿势之前尝试一下。但是,我查看了帮助视频,他们通过开始代码来解决问题

def print_cheapest_shipping_method(weight):

print(
    "The cheapest option available is $%.2f with %s shipping")

我有点理解代码继续将它们定义为变量,但是如果有人可以更清楚地向我解释一下?到目前为止,课程中还没有像 %.2f 和 %s 这样的东西,我想更好地了解讲师是如何得出这些数字的。

编辑添加:我的解决方案在下面再次编辑,这是整个页面

    def ground_ship(weight):
  if weight >= 10:
    cost = 4.75
  elif weight >= 6:
    cost = 4
  elif weight >= 2:
    cost = 3
  elif weight >= 0:
    cost = 1.50  
  print(weight * cost + 20)

premium_ship = 125

def drone_ship(weight):
  if weight >= 10:
    cost = 14.25
  elif weight >= 6:
    cost = 12
  elif weight >= 2:
    cost = 9
  elif weight >= 0:
    cost = 4.5
  print(weight * cost)

ground_ship(10)
drone_ship(1.5)

def best_deal(weight):
  if ground_ship < drone_ship and ground_ship < drone:
    method = "standard ground"
    cost = ground_ship
  elif premium_ship < drone_ship and premium_ship < ground_ship:
    method = "premium"
    cost = premium_ship
  else:
    method = "drone"
    cost = drone_ship

    print("The cheapest option for your package is " + method + " shipping and the cost will be $" + str(cost))

best_deal(10)

一切检查都没有错误,直到我尝试使用 best_deal(10) 打印,然后返回

Traceback (most recent call last):
  File "script.py", line 41, in <module>
    best_deal(10)
  File "script.py", line 29, in best_deal
    if ground_ship < drone_ship and ground_ship < drone:
TypeError: unorderable types: function() < function()

忽略行 # 此代码从第 28 行开始

标签: python

解决方案


您的比较语句有效地比较了函数,因此出现了错误。您打算做的是比较每个函数计算的结果。为此,您需要对每个函数进行一些更改,以便它们返回计算出的成本。打印它是不够的。因此,考虑到这一点,代码的工作版本如下所示。一会我会添加评论。

def print_cheapest_shipping_method(weight):
  print("The cheapest option available is $%.2f with %s shipping")

def ground_ship(weight):
  if weight >= 10:
    cost = 4.75
  elif weight >= 6:
    cost = 4
  elif weight >= 2:
    cost = 3
  elif weight >= 0:
    cost = 1.50
  total_cost = weight * cost + 20
  print(total_cost)
  return(total_cost)  # have the function return the calculated value

premium_ship = 125

def drone_ship(weight):
  if weight >= 10:
    cost = 14.25
  elif weight >= 6:
    cost = 12
  elif weight >= 2:
    cost = 9
  elif weight >= 0:
    cost = 4.5
  total_cost = weight * cost
  print(total_cost)
  return(total_cost)  # have the function return the calculated value

ground_ship(10)
drone_ship(1.5)

def best_deal(weight):
  # now you can compare values, by calling each function with the given 'weight'
  if ground_ship(weight) < drone_ship(weight) and ground_ship(weight) < premium_ship:
    method = "standard ground"
    cost = ground_ship(weight)  # get the cost from the function calculation

  elif premium_ship < drone_ship(weight) and premium_ship < ground_ship(weight):
    method = "premium"
    cost = premium_ship  # in this case, premium_ship is a value
  else:
    method = "drone"
    cost = drone_ship(weight)  # get the cost from the function calculation

  print("The cheapest option for your package is " + method + " shipping and the cost will be $" + str(cost))

best_deal(10)

也可以在这里看到它的实际效果:https ://eval.in/1116417


推荐阅读