The zero was not being captured!¶
Version 1:¶
#find the minimum absolute value of a list
data = [6, 1, -10, -7, 0, 6, -9, 7, 1, -2]
lowest = 1e100 #set to a very big number (a "googol": 1x10^100)
#iterate through the data
for d in data:
#if it's positive and less than the lowest so far
if d >= 0 and d < lowest:
#replace the lowest so far with the current value
lowest = d
if d < 0:#if less than zero
#negate
d = -d
if d < lowest:
lowest = d
print(lowest)
0
Version 2:¶
#find the minimum absolute value of a list
data = [6, 1, -10, -7, 0, 6, -9, 7, 1, -2]
lowest = 1e100 #set to a very big number (a "googol": 1x10^100)
#iterate through the data
for d in data:
if d < 0:#if less than zero
#negate
d = -d
if d < lowest:
lowest = d
#if it's positive and less than the lowest so far
elif d < lowest:
#replace the lowest so far with the current value
lowest = d
print(lowest)
0