Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python成本检查(已关闭)_Python_Python 3.x - Fatal编程技术网

Python成本检查(已关闭)

Python成本检查(已关闭),python,python-3.x,Python,Python 3.x,我正在做一个函数,输入权重并返回成本。我的代码是正确的,我做了计算,但返回的成本没有任何意义 def cost_check(weight): if weight <= 2: cost = (weight * 1.50) + 20.00 elif weight > 2: cost = (weight * 3.00) + 20.00 elif weight > 6: cost = (weight * 4.00) + 20.00 else:

我正在做一个函数,输入权重并返回成本。我的代码是正确的,我做了计算,但返回的成本没有任何意义

def cost_check(weight):
  if weight <= 2:
    cost = (weight * 1.50) + 20.00
  elif weight > 2:
    cost = (weight * 3.00) + 20.00
  elif weight > 6:
    cost = (weight * 4.00) + 20.00
  else:
    cost = (weight * 4.75) + 20.00
    
  return cost

print(cost_check(8.4))
def成本检查(重量):
如果重量为2:
成本=(重量*3.00)+20.00
elif重量>6:
成本=(重量*4.00)+20.00
其他:
成本=(重量*4.75)+20.00
退货成本
打印(成本检查(8.4))
当我调用函数时,它应该返回53.60。但由于某些原因,它返回45.2,这确实令人沮丧,所以我将它改为:

def cost_check(weight):
  cost = 0
  if weight <= 2:
    cost = (weight * 1.50) + 20.00
  elif weight > 2 and weight <= 6:
    cost = (weight * 3.00) + 20.00
  elif weight > 6 and weight <= 10:
    cost = (weight * 4.00) + 20.00
  else:
    cost = (weight * 4.75) + 20.00
    
  return(cost)
def成本检查(重量):
成本=0

如果重量2和重量6以及重量这不起作用,因为这些陈述永远不会达到:

  elif weight > 6:
    cost = (weight * 4.00) + 20.00
  else:
    cost = (weight * 4.75) + 20.00
假设你的体重=3。然后它将匹配语句

 elif weight > 2:
    cost = (weight * 3.00) + 20.00

因为接下来的语句是elif,否则它们将被跳过

8.4>2
,所以你得到的是
(权重*3.00)+20.00
ooh所以如果我对每个语句都设置了一个限制,那么它应该有效吗?在检查
>6
之前,我会先检查
>2
>6
代码将永远不会执行。你可以把
>6
块放在第一位;当前无法访问
elif-weight>6:
else: