Python Forum
Python h/w help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Python h/w help (/thread-224.html)



Python h/w help - tahmid909 - Oct-01-2016

Hi all,
can i get a bit of help with my H/W, the task is as follows:
Develop a program that analyses a sentence that contains several words without punctuation. When a word in that sentence is input, the program identifies all of the position the word occurs in the sentence. The system should not be case sensitive: Ask, ask & ASK should be treated as the same word.
For example, in the sentence                                                                                                                               ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY
The word ‘COUNTRY’ occurs in the 5th and 17th positions.
Analyse the requirements for this system and design, develop, test and evaluate a program to locate and return the position(s) of the word you have selected in a particular sentence or return an error message if the word is not in the sentence.
 
Heres what i've done so far -

sentence= input ("Enter a sentence that contains several words, without punctuation.")

print(sentence)

sentence.split(' ')

word=input("please enter a word from the sentance and its position shall be displayed")

if word in sentence:

    print ("found word")

else:

    print ("word not found")
My problem is that the code wont tell me the position of a word in the sentence and that the code is not case - insensitve.
Any help would be appreciated.



RE: Python h/w help - j.crater - Oct-01-2016

To make it case insensitive, I suggest you first change sentences or words you want to analyse to lower case (e.g. using lower() method).

The result of sentence.split(' ') would best be stored in another variable (named e.g. split_sentence), and you'll have a list. To get index (location) of an item in a list, you can use .index() method.

P.S.: use code tags next time for parts of your post that contain code ;)


RE: Python h/w help - tahmid909 - Oct-01-2016

ok, thanks that helps me with the case-insensitve problem


RE: Python h/w help - wavic - Oct-01-2016

list.index(obj)
will return the index of the first appearance of obj.