-5
loop = True
while loop yes no maybe
sentence is : ", myList.index(wordchosen) + 1)

sorry i had to do this :(

Lils yet
  • 1
  • 3
  • 3
    If you want help here, please try writing a coherent question in plain english. – wim Jan 07 '16 at 18:32
  • Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Morgan Thrapp Jan 07 '16 at 18:36

3 Answers3

0

I think this is what you want

while True:
    sentence = input('Enter a sentence:\n')
    sentence = sentence.lower().split()
    print(sentence)


    word = input('Enter a word you want to find in the sentence:\n')

    print([counter for counter, ele in enumerate(sentence) if word == ele])

    decision = input('Yes or no to exit or continue')
    decision.lower
    if decision == 'yes':
        continue
    else:
        break

This returns a list of all the indexes of the word you're looking for in the sentence sentence.index() will not work because it will return the item position of the first element in the sentence

to get a visual occurrence of the words in the list just change the print to

print([counter + 1 for counter, ele in enumerate(sentence) if word == ele])
danidee
  • 9,298
  • 2
  • 35
  • 55
0

Your question was very confusing because it contained only code which wasn't styled as code. You should always include some plain English description of the problem. I tried to guess :)

Your code has some indentation issues, it doesn't run as such.

You use the input() function, which fails when a sentence is entered:

write a sentence
tre sdf fre dfg
Traceback (most recent call last):
  File "bin/tst.py", line 14, in <module>
    sentence = input()
  File "<string>", line 1                  
    tre sdf fre dfg
          ^                                
SyntaxError: invalid syntax

From the input() documentation:

Consider using the raw_input() function for general input from users.

After switching from input() to raw_input() another issue pops up: the code doesn't appear to work. That's because you're using the is identity operator to check equality, which won't always produce the result you're expecting. See: Why does comparing strings in Python using either '==' or 'is' sometimes produce a different result?

After switching is to == the code works as you probably intended it:

write a sentence
this is the input sentence
this is the input sentence
choose a word from the sentence that you would like to find the word postion of
sentence
('word position for your sentence is : ', 5)
write a sentence

Side note: you don't need to use paranthesis around variables: (wordchosen).

There are more efficient/elegant/pythonic ways of checking if wordchosen is in myList, see the other answers.

Community
  • 1
  • 1
Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
-1

I'm not quite sure what you are asking but this psuedocode should be somewhat like what you (likely) want to do.

Try something such as this:

  1. get the users string and word they want to search for
  2. parse sentence into an array (parse them by spaces)
  3. loop through they array and increase a count for each time the word appears
  4. save the positions of where these words appear into another array that you can later use or return
Alex1620
  • 255
  • 1
  • 16