Reputation: 3
I'm new to programming. I have been starting to learn it from Grok (website that teaches python). But I got stuck on one of the questions.
This is the question from Grok:
Write a program where you can enter one word at a time, and be told how many unique words you have entered. You should not count duplicates. The program should stop asking for more words when you enter a blank line.
For example:
Word: *Chat* Word: *Chien* Word: *Chat* Word: *Escargot* Word: You know 3 unique word(s)!
and
Word: *Katze* Word: *Hund* Word: *Maus* Word: *Papagei* Word: *Schlange* Word: You know 5 unique word(s)!
and
Word: *Salam* Word: You know 1 unique word(s)!
I have tried like for 4 days but I can't figure it out. This is my terrible code so far.
list1 = []
word = input('Word: ')
while word != '':
list1.append(word)
word = input('Word: ')
for w in list1:
for l in range(len(list1)):
if w != list1[l]:
s = list1[l]
list1.remove(s)
print('You know', len(list1), 'unique word(s)!')
Upvotes: 0
Views: 1385
Reputation: 55469
As others have said, the simple & efficient way to do this is to use a set to store the words because a set cannot hold duplicate items.
But it's possible to do it with lists too. Rather than making things complicated and trying to remove duplicates from the finished list, you can just test each word to see if it's in the list before adding it. Eg,
list1 = []
word = input('Word: ')
while word != '':
if word not in list1:
list1.append(word)
#print(list1)
word = input('Word: ')
print('You know', len(list1), 'unique word(s)!')
When debugging code it can be helpful to insert print
calls at strategic places to make sure your variables contain what you expect them to do, which is why I added the print(list1)
line. If you un-comment that line it will show you the list contents on every loop.
Here's a slightly better version that doesn't need two identical input
calls:
list1 = []
while True:
word = input('Word: ')
if not word:
break
if word not in list1:
list1.append(word)
print('You know', len(list1), 'unique word(s)!')
Upvotes: 0
Reputation: 11717
You should try Python set, to ensure you have unique elements. set
is a container with unordered unique elements.
Below is your algorithm where all inputs are appended to your initial list. Then we create a set from your list, where we count unique number of elements.
list1 = []
word = input('Word: ')
while word != '':
list1.append(word)
word = input('Word: ')
print('You know', len(set(list1)), 'unique word(s)!')
Upvotes: 0
Reputation: 17263
Instead of using a list
use set
to store the words. Set is an unordered collection of unique items which state won't change if you add same item multiple times:
s = set()
word = input('Word: ')
while word != '':
s.add(word)
word = input('Word: ')
print('You know', len(s), 'unique word(s)!')
Example output:
Word: foo
Word: bar
Word: foo
Word:
You know 2 unique word(s)!
Note that your original code was removing elements from list
while iterating over it. You shouldn't modify the sequence while iterating over it since the behavior is undefined.
Upvotes: 1