Skip to content Skip to sidebar Skip to footer

How To Add Integer Input In A Set Object

unable to add an item into a set taking it as an input from user input>>j=set() input>>j.add(int(input())) 4 TypeError: descriptor 'add' requires a 'set' object but re

Solution 1:

2 ways to do it:

  1. j = set()j.add(a) # a can be anything. If you want integer you can type cast it with int()

  2. 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

Solution 2:

It because you have to call it to get a set

j = set()
j.add(int(input()))

Post a Comment for "How To Add Integer Input In A Set Object"