Reputation: 71
unable to add an item into a set taking it as an input from user
input>>j=set()
input>>j.add(int(input()))
TypeError: descriptor 'add' requires a 'set' object but received a 'int'
Upvotes: 0
Views: 1909
Reputation: 31
2 ways to do it:
j = set()
j.add(a)
# a can be anything. If you want integer you can type cast it with int()
j = {''}
# initializing a set in this way requires some default parameter otherwise python will make it a dictionary
j.add(1)
or
j.add(a)
# where a is an integer
j.remove('')
# remove the initial string that we added
Upvotes: 1
Reputation: 1610
It because you have to call it to get a set
j = set()
j.add(int(input()))
Upvotes: 0