请高手帮忙看一下这个函数的问题
codecombat吧
全部回复
仅看楼主
level 1
sdlkqukun 楼主
def findNearestPair(units):
minDistance = 9001
nearestPair = ["Nobody", "Nobody"]
# You need to check and compare all pairs of units.
# Iterate all units with indexes "i" from 0 to "len(units)".
for i in range(len(units)):
pairOfNamesI = units[i]
# Iterate all units again with indexes "j".
for j in range(len(units)):
# If "i" is equal to "j", then skip (continue).
if i == j:
continue
pairOfNamesJ = units[j]
# Find the distance between the i-th and j-th units.
dist = pairOfNamesI.distanceTo(pairOfNamesJ)
# If the distance less than 'minDistance':
if dist < minDistance:
# Reassign 'minDistance' with the new distance.
minDistance = dist
# Reassign 'nearestPair' to the names
nearestPair = [pairOfNamesI,pairOfNamesJ]
# of the current pair of units.
return nearestPair
这个函数的作用应该是循环检查两个角色之间的距离,如果距离小于最短距离,则将该距离交给变量最小距离直到找到存在最短距离的两个角色,输出两个角色的名字。现在我的函数只能检查并输出第一对角色距离并输出角色名字,不能输出最短距离的那两个,请高手给我修改下呗!
2018年02月27日 13点02分 1
1