我对python有点陌生,最近遇到了一个我无法理解的问题,即如何在不使用python中的min或max函数的情况下找到列表的最小值。以下是我的代码:
#Prompt user to enter a minimum of 5 round trip times separated with commas
roundTrips = input('Please enter at least 5 round trips seprated by ",": ')
#Check closest object
def closest_object(roundTrips):
roundTrips = list(roundTrips.split(','))
count = 0
minimum = 0
if len(roundTrips)<=4:
print('error')
else:
while count<len(roundTrips):
positions = roundTrips[count]
count += 1
if minimum or (int(positions)<minimum):
minimum = positions
print(minimum)
#Perform the parsing of roundTrips input here
closestObject = []
print('The closest object is',closestObject) #Modify to display the closest object
return closestObject#Do not remove this line
这是可行的,但我不认为这样简单的任务,你需要问关于stackoverflow的问题。你可以在网上找到许多类似的答案
如果您对代码失败的原因而不是复制粘贴感兴趣,请回答:
我不知道}短路,}。在
if minimum
应该完成什么,但它所做的是每次minimum != 0
执行循环,因为它的计算结果是True
,并且由于{int(positions) < minimum
没有被检查,所以只需删除{你应该将最小值初始化为列表的第一个元素,因为你不能确定0在其中。在
相关问题 更多 >
编程相关推荐