为什么print语句的值实际上没有打印出来。 。 。没有显示语法错误代码

时间:2019-04-09 19:39:04

标签: python

我制作了一个函数,以获取最便宜的送货方式,据我所知,我的代码看起来正确,但是该值未打印且没有错误代码。

如果认为缩进可能与它有关,但这似乎无法纠正

 def cheapest_shipping_method(weight):

  ground = ground_shipping(weight)
  premium = premium_ground_shipping
  drone = drone_shipping_costs(weight)

  if ground < premium and ground < drone:
    method = "standard ground"
    cost = ground
  elif premium < ground and premium < drone:
    method = "premium ground"
    cost = premium
  else: 
    method = "drone shipping"
    cost = drone

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

  print_cheapest_shipping_method(4.8)
  print_cheapest_shipping_method(41.5)

期望以列出的值(4.8和41.5)查看最便宜的运输方式。

1 个答案:

答案 0 :(得分:0)

您的核心cheapest_shipping_method方法似乎不错。为了获得合理的结果,我根本不需要更改它。猜测您要在此处执行的操作,然后任意添加缺少的函数和值,这将为您的两个输入运行并打印不同的值:

def ground_shipping(weight):
    return 2 * weight

premium_ground_shipping = 11.5

def drone_shipping_costs(weight):
    return 10 * weight

def cheapest_shipping_method(weight):

  ground = ground_shipping(weight)
  premium = premium_ground_shipping
  drone = drone_shipping_costs(weight)

  if ground < premium and ground < drone:
    method = "standard ground"
    cost = ground
  elif premium < ground and premium < drone:
    method = "premium ground"
    cost = premium
  else:
    method = "drone shipping"
    cost = drone

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

cheapest_shipping_method(4.8)
cheapest_shipping_method(41.5)

结果:

The cheapest option is $9.60 with standard ground shipping.
The cheapest option is $11.50 with premium ground shipping.

因此,您只需要将对函数的调用移到函数之外,并使名称匹配。