prashant jeena
prashant jeena

Reputation: 71

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 received a 'int'

Upvotes: 0

Views: 1909

Answers (2)

Saurabh Adhikary
Saurabh Adhikary

Reputation: 31

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

Upvotes: 1

EvensF
EvensF

Reputation: 1610

It because you have to call it to get a set

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

Upvotes: 0

Related Questions