Anonymous asked in Computers & InternetProgramming & Design · 8 months ago

I need help with my coding homework?

I have to make a code that reports how many words contain the word owl, it has to report the indices at which the words occurred. This is my code:

word = "owl"text = input("Enter some text: ")text = text.lower()text = text.split()amount = text.count(word)print "There are " + str(amount) + " words that contained \"owl\"."print "They occurred at indices: "for index, word in enumerate(text):    print text


The output of this is:

Enter some text: owl is cool

There are 1 words that contained "owl".

They occurred at indices:

['owl', 'is', 'cool']

['owl', 'is', 'cool']

['owl', 'is', 'cool']

but it's supposed to be something like this:

Enter some text: Owls are so cool! I think snowy owls might be my favorite. Or maybe spotted owls.

There were 3 words that contained "owl".

They occurred at indices: [0, 7, 15]

2 Answers

  • EddieJ
    Lv 7
    8 months ago
    Favourite answer

    Well, Rose, the first thing I tried to teach you is that you should start your headline with "Python", but, later I told you to start with "Python2" or "Python3".

    You code is ambiguous.  Python 2 uses raw_input () and a print statement (without parens).  Python 3 uses input () and print ().  Python 2 DOES have an input () function but it will try to evaluate your input as an expression.

  • 8 months ago

    About "it has to report the indices at which the words occurred", is that the index of "owl" in the word, the index of the word in the word list, or the index of the word in the original input string?

    About the code in the question: For now, you'll need to carefully edit questions.  The copy-paste feature of the editor here doesn't handle newlines properly.  It deletes them sometimes and doubles them at other times.  Cleaning that up, I see:

    word = "owl"

    text = input("Enter some text: ")

    text = text.lower()

    text = text.split()

    amount = text.count(word)

    print("There are " + str(amount) + " words that contained \"owl\".")

    print("They occurred at indices: ")

    for index, word in enumerate(text):   


    I added parentheses to the print function calls, since print() is a function, not a statement in Python 3.  Otherwise, it's just your code as I read it.

    There are a few problems besides the print() calls:

    1. The text.count(word) will give you a count of how many words were equal to 'owl', not a count of how many word contained 'owl'.

    2. You're using the variable name "word" for two different purposes: The word to search for, and a loop variable.

    3. You don't save the index of words that contained 'owl' while you're trying to count the occurrences, so to find those indices you're going to have to search the words twice.

    Here's a suggestion.  Forget about .count() and instead create a list of indices of words containing 'owl'.  Then the number of words containing 'owl' will simple be the len() of that list.  And you can print out the indices with a simpler loop; or you can use one of the nice features of Python 3 function calls, and the print() function, to get the final output in a single line:

    print("Number of words containing 'owl':", len(index_list))

    print("Indices of words with 'owl':", *index_list)

    The best way to find out if 'owl' occurs in a string named the_word is to use the "in" operator:

    if 'owl' in the_word:


    Put that in a loop enumerating the words in the input text, with indices, and use your favorite variable names.  That'll give you your index list.  (Obviously, that list has to be set to an empty list before you begin looping.)

Still have questions? Get answers by asking now.