• nearest_value

 

Hi everyone! I have a problem with nearest_value task my code is:

def nearestvalue(values: set, one: int) -> int: # your code here difval = [] listval = [] mindif = "" minindex = 0 valmin = "" minsindex = [] valsmins = []

for i in values:
    list_val.append(i)
for n in list_val:
    dif_val += [abs(abs(n) - abs(one))]
    min_dif = min(dif_val)
if dif_val.count(min_dif) == 1:
    min_index = dif_val.index(min_dif)
    val_min = list_val[min_index]
    return val_min
else:
    min_index = dif_val.index(min_dif)
    val_min = list_val[min_index]
    for j in range(len(dif_val)):
        if dif_val[j] == min_dif:
            mins_index.append(dif_val.index(j))
        for l in mins_index:
            vals_mins.append(list_val[l])
            val_min = min(vals_mins)
            return val_min

I think my solution is well, but when I try to check my result, it shows me this error :

ValueError: 0 is not in list, nearestvalue, 25 Fail: nearestvalue([1,3,4,5,6,7],2)

What's wrong?