也许我从根本上误解了 python 中的缩进? - 蟒蛇



这段代码在检查时给了我一个缩进错误。我知道这种情况经常发生,但该实例介于存在的两个 for 循环之间,因为我需要引用两个不同的列表。

我什至还没有数据集,但它应该报告语法至少是正确的。代码相当简单。我想在建筑物中自动放置包裹,我想通过采用最大的包裹并将它们放置在仍然适合的空间最少的空间中来实现。

到目前为止,我使用的所有输入都是字典,因为我也需要知道我指的是哪个书架。我非常接近将其转换为列表,并且对格式非常严格。

inv = maxkey["Inventory"]

是发生错误的行。我不知道如何解决它。我应该改用此项目的列表吗?逻辑有缺陷吗?有没有我忘记的括号?如果这只是我的疏忽,请告诉我。请联系我了解更多详情。

def loadOrder(inProd, units, loc, pref, shelves):
items = len(inProd)
while items > 0
# What is the biggest package in the list?
mxw = 0 # Frontal area trackers
BoxId = {} # Identifies what is being selected
for p in inProd:
if p["Height"]*p["Width"] > mxw:
mxw = p["Width"]*p["Height"]
BoxId = p
else:
pass
# What is the location with the least amount of space?
maxi = 0.001
maxkey = {}
for key in loc:
if key["Volume Efficiency"] > maxi and key["Width"] > mxw/BoxId["Height"]:
maxi = key["Volume Efficiency"]
maxkey = key
else:
pass
maxkey["Inventory"].append(BoxId)
weight = 0
volTot = 0
usedL = 0
inv = maxkey["Inventory"]
for k in inv:
weight = k['Weight']+weight
vol = k['Height']*k['Width']*k['Depth']+volTot
usedL = k['Width']+usedL
maxkey["Volume Efficiency"] = volTot/(maxkey['Height']*maxkey['Weight']*maxkey['Depth'])
maxkey['Width Remaining'] = usedL
maxkey['Capacity Remaining'] = weight
del inProd[BoxId]
items = len(inProd)
return [inProd, units, loc, pref, shelves]

函数定义中的缩进应如下所示:

def function-name():
<some code>
<return something>

此外,您错过了:之后的循环条件。

应该是while items > 0:

而且您不应该混合使用制表符空格进行缩进。 缩进的标准方式是 4 个空格。 您可以在 PEP 8 中看到更多信息。

最新更新