-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?

ChristianYami
  • 586
  • 1
  • 9
  • 17
Nathan Chan
  • 147
  • 1
  • 13

5 Answers5

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)!")
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
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)))
Exelian
  • 5,749
  • 1
  • 30
  • 49
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.

damaredayo
  • 1,048
  • 6
  • 19
-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:
#...
mrbTT
  • 1,399
  • 1
  • 18
  • 31
  • It's not a good idea to encourage bad code writing habits such as this. – damaredayo Mar 16 '20 at 14:20
  • 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. – mrbTT Mar 16 '20 at 14:22
  • 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. – damaredayo Mar 16 '20 at 14:24
-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)!")

Sanjeet Mehta
  • 37
  • 1
  • 7
  • 1
    Why encourage bad practice like this? There is no need to iterate `listword` in order to check if it contains `word`. – dspencer Mar 16 '20 at 14:39