-2

My question is to ask user input a world at a time and see how many unique world the user know (duplicate word wont count) e.g.

Word: Chat
Word: Chien
Word: Chat
Word: Escargot
Word: 
You know 3 unique word(s)!

below is what I have now:

count = 0
listword = []
word = input("Word: ")
while word != "":
    for i in listword:
            if i != word:
            listword.append(word)
        count += 1
    word = input("Word: ")
print("You know "+count+"unique word(s)!")

however the output is like this:

Word: hello
Word: hi
Word: hat
Word: 
You know  0  unique word(s)!

How can I adjust my code and why is count still =0?

5
  • 1
    Look at the set class Commented Mar 16, 2020 at 14:13
  • You could use a set here instead of a list and return length of that set. Commented Mar 16, 2020 at 14:13
  • Have you heard about collections.Counter? Commented Mar 16, 2020 at 14:13
  • 2
    @ZavenZareyan Not really useful here. Using a set is enough and efficient. Commented Mar 16, 2020 at 14:14
  • Does this answer your question? Get unique values from a list in python Commented Mar 16, 2020 at 21:20

5 Answers 5

5

The problem is that listword is initially empty, and nothing ever gets added unless the entered word doesn't match a word already in listword. What you really want to do is to add the word if it isn't found in listword.

You could do that with a list, but a set would be more efficient:

listword = set()
word = input("Word: ")
while word.strip() != "":
    if word not in listword:
        listword.add(word)
    word = input("Word: ")
print("You know", len(listword), "unique word(s)!")
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! never heard about set before so thats why I didn't know how to do it
1

I would suggest using collections.Counter. This provides a simple an pythonic way to calculate total counts and is provided in the standard lib.

You could use it like this:

from collections import Counter

total_counts = Counter()
word = input("Word: ")
while word:
    total_counts.update([word])
    word = input("Word: ")
print("You know {:d} unique word(s)!".format(len(total_counts)))

4 Comments

How does this solve the given problem better than the builtin set?
It doesn't solve it better, but it works and provides the ability to expand the usage of the script. Also I like to advertise python built-ins.
Feel free to provide an answer with a set implementation. I'll happily upvote it if it's clean.
I added an answer.
0

Editing your code you can just do:

listword = []
word = input("Word: ")
while word: # empty strings are equal to false as a boolean, and anything in them is equal to true
    if word not in listword:
        listword.append(word)
    word = input("Word: ")
print("You know ",len(listword),"unique word(s)!")

Although I would look into a more pythonic way of doing this if I were you.

1 Comment

more pythonic way means using set? never heard about it before so didn't know how to do it but thanks anyway!
-2

Since at the start you declare an empty list:

listword = []

And only append items to it inside this loop:

for i in listword:

You'll never enter this loop, because the list will always be empty, thus you'll never cycle through count += 1.

So you should add another check to see if list is empty:

while word != "":
    if len(listword) == 0:
        listword.append(word)
        count+=1
    for i in listword:
            if i != word:
#...

3 Comments

It's not a good idea to encourage bad code writing habits such as this.
It's not a good idea to downvote if you don't agree. Even thou his code is not the greatest, my goals was to pinpoint what he was doing wrong, so he could learn and then improve his coding. Just giving a good code block without explaining what was wrong is a bad habit.
The code is poor; its not a case of disagreement, its a case of what you're teaching them to do is not good practice and so downvoting the answer lessens the chance that they will see or attempt to use it.
-2

This code is same as yours with slight modification.

count = 0
listword = []
word = input("Word: ")

while word != "":
    found= False
    for i in listword:
        if i == word:
            found= True
    if not found:
        listword.append(word)
    word = input("Word: ")
print("You know "+str(len(listword))+" unique word(s)!")

1 Comment

Why encourage bad practice like this? There is no need to iterate listword in order to check if it contains word.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.