I had a code which started using the .add() method with the set, like this

def nearest_value(values: set, one: int) -> int:
# If the number is in the set, return the number,
# otherwise put it in the set.
if one in values:
return one
else:
values.add(one)
svalues = sorted(values)

Upon checking I got this failure message:

AttributeError: 'list' object has no attribute 'add', nearest_value, 7

Fail: nearest_value([4,7,10,11,12,17],9)

It looks like it's supposed to work when the input is a list instead of a set. Surely that is incorrect?

To come around it, I just started my code by making the list and then used .append() on the list instead of .add() on the set. An also, of course, there are many more elegant solutions. But I still think that the square brackets should be changed to curled brackets in the check.

Created at: 2020/05/15 11:01; Updated at: 2021/12/04 23:11