Home > Mobile >  Why doesn't my code detect any variable which I input into this list when using .index(), when
Why doesn't my code detect any variable which I input into this list when using .index(), when

Time:09-18

wordList = []
counter = 0
y = 1
total = 0
wordTotal = 0
while y == 1:
    word = input("enter words\n")
    continued = input("do you want to continue? y or n ")
    if continued == "n":
        y = 0
    total = total   1
    newWords = []
    wordList.append(word)
    wordCount = wordList.count(word)
    totals = []
    if wordCount > 1:
        wordTotal = wordTotal - 1
        whichWord = newWords.index(word)
        totals[whichWord] = totals[whichWord]   1
    if wordCount == 1:
        wordTotal = total - wordTotal
        newWords.append(word)
        print(newWords)
        totals.append(1)
        print(totals)
    if wordTotal == 0:
        wordTotal = 1

print("the number of different words is", wordTotal)

This program takes user inputted words, and counts how many repetitions of certain words there are, and how many unqiue words there are. In the second if statement in my code, when I try to index through the array newWords[], and update the value of a repeated word from 1->2, 2->3 etc.., it says ValueError: '...' is not in list. However, when i print the newWords list out in the third if statement, the value is there.

So sorry if I have made an obivous mistake - I am relatively new to python, all help is hugely appreciated :D.

CodePudding user response:

The first time you enter inside this if

if wordCount > 1:
        wordTotal = wordTotal - 1
        whichWord = newWords.index(word)
        totals[whichWord] = totals[whichWord]   1

word is equal to the 2nd word entered by the user, you agree? but since the only time you're adding a word inside newWords is in these lines :

if wordCount == 1:
        wordTotal = total - wordTotal
        newWords.append(word)
        print(newWords)
        totals.append(1)

when you get to the if wordCount > 1: above, the only word inside newWords is the first word the user did input, therefore the 2nd word isn't in it, and ence why your error

Let's do a run together : Users input for instance "hello", so hello is inside word, you add it to

wordList.append(word)

and wordcounts = 1, so you go inside the if loop

if wordCount == 1:
            wordTotal = total - wordTotal
            newWords.append(word)
            print(newWords)
            totals.append(1)

now newWords = ["Hello"]

Now users input another word, for instance "World" so now

word = "World" wordCounts = 2

but

  newWords = ["Hello"]

because you never added "World" to it but in

if wordCount > 1:
            wordTotal = wordTotal - 1
            whichWord = newWords.index(word)
            totals[whichWord] = totals[whichWord]   1

you're trying to access the word "World" inside newWords

CodePudding user response:

If you're using Python 3.8 you can use the following code.

The loop can be controlled based on the user input. If user just presses RETURN then the value returned by input() will be an empty string which is 'falsy'. Otherwise append the input word to a list.

Subsequently, the number of entered words is just the length of the list and the number of unique words is the length of a set constructed from that list.

Therefore, all you need is:

list_of_words = []

while (word := input('Enter a word or <return> to end: ')):
    list_of_words.append(word)

print(f'Your entered {len(list_of_words)} words of which {len(set(list_of_words))} are unique')
  • Related