can some one please explain to me how I could go about converting a set to an int, its homework
when i need to compare the length of the set to the length of a list or something else?
lst = []
oldset =set()
word_set = {}
while True:
inp = input('Enter text: ')
if inp == '':
print('Finished')
break
lst = inp.split()
for word in lst:
oldset.add(word)
oldset = len(oldset)# im sure that this line is my error it tells me to remove .add but i need that
if word_count < len(word_set):
word_count[word] = len(word_set.keys())
print(word,word_count)
the error message i am receiving is
Traceback (most recent call last):
File "./input_counter.py", line 17, in <module>
oldset.add(word)
AttributeError: 'int' object has no attribute 'add'
I'm not sure what you're trying to accomplish with this code:
But what you are actually accomplishing is as follows: you loop through all the words in
lst
, and for each word, you try to add the word tooldset
, and then you demolisholdset
and replace it with anint
-- the length ofoldset
. This obviously only works once, because after you do it once,oldset
is no longer aset
, but is now anint
.Understand that a
set
is a container -- it contains many other things -- while anint
is simply a value -- it's just a number. What are you trying to do here? Tell us more...