Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python h/w help
#1
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.
Reply
#2
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 ;)
Reply
#3
ok, thanks that helps me with the case-insensitve problem
Reply
#4
list.index(obj)
will return the index of the first appearance of obj.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020